1
>>> save = lambda : {'a':[[0]]*10}
>>> s = save()
>>> s
{'a': [[0], [0], [0], [0], [0], [0], [0], [0], [0], [0]]}
>>> s['a'][-1].append(1)
>>> s
{'a': [[0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1]]}

I tell python to add 1 to the last place but it seem's that each element are same.

Henry
  • 577
  • 4
  • 9

1 Answers1

0

You created one object, [0], and repeated this object 10 times. All your changes are changing this single object.
Instead, you should create 10 different objects.

save = lambda : {'a':[[0] for i in range(10)]}
SherylHohman
  • 16,580
  • 17
  • 88
  • 94
dimay
  • 2,768
  • 1
  • 13
  • 22
  • While this code may resolve the OP's issue, it is best to include an explanation as to how your code addresses the OP's issue. In this way, future visitors can learn from your post, and apply it to their own code. SO is not a coding service, but a resource for knowledge. Also, high quality, complete answers are more likely to be upvoted. These features, along with the requirement that all posts are self-contained, are some of the strengths of SO as a platform, that differentiates it from forums. You can edit to add additional info &/or to supplement your explanations with source documentation. – SherylHohman May 21 '20 at 17:05
  • 1
    You create one object [0] and repeat this object 10 times. All your changes have changing this object. You should create 10 different objects. – dimay May 21 '20 at 18:36