0

In the code below, very strangely to me, 0 and 1 are added to the BOTH keys:

zip_to_dict1 = zip(["a", "b"], [[]]*2)
dict1 = dict(zip_to_dict1)
dict1["a"].append(0)
dict1["b"].append(1)
print(dict1["a"], dict1["b"])
# [0, 1] [0, 1]

However, this works somehow:

zip_to_dict2 = zip(["a", "b"], [[], []])
dict2 = dict(zip_to_dict2)
dict2["a"].append(0)
dict2["b"].append(1)
print(dict2["a"], dict2["b"])
# [0] [1]

Is there any difference between [[]]*2 and [[], []]?:

[[]]*2 == [[], []]
# True

...What's the problem?

(Note: I'm using Python 3.4.1)

akai
  • 2,498
  • 4
  • 24
  • 46
  • How can I close the question? – akai Feb 28 '15 at 05:36
  • 1
    Don't worry about it. Nobody can answer the question, and even though your question is marked as a duplicate it may be helpful to people with a similar question doing a search. – PM 2Ring Feb 28 '15 at 05:39

0 Answers0