I ran into a rather weird problem with python list appending today. I was trying to create an array whose each element would be like a C struct. One of these elements was a list by itself. This is the problematic code:
class players:
name='placeholder'
squad=list()
teams=list()
teams.append(players())
teams.append(players())
teams[0].name="abc"
teams[1].name="xyz"
teams[0].squad.append("Joe")
for w in teams:
print(w.name)
print(w.squad)
The output I expected is:
abc
['Joe']
xyz
[]
Since I only added a member to squad for teams[0]. But the output I get is:
abc
['Joe']
xyz
['Joe']
The name is set fine but the .append appended it to both elements of teams!
What causes this and how can I work around this?