Short answer: because dict
objects are mutable, and int
objects are immutable.
Details:
Look at [{'a': True, 'b': True}] * 3
With
>>> l = [{}] * 3
you create list which contains reference to the same object 3 times.
>>> id(l[0])
139685186829320
>>> id(l[1])
139685186829320
>>> id(l[2])
139685186829320
So when you change one of them, you change them all (in case of mutable objects).
If you want list of different dictionaries, you can do it with:
>>> l = [{} for x in range(3)]
>>> id(l[0])
139685161766216
>>> id(l[1])
139685161766536
>>> id(l[2])
139685161766600
In your case it should look like this:
a = dict(zip([1, 2, 3], [{'a': True, 'b': True} for i in range(3)]))
With immutable objects it is different.
You cannot change immutable object. Everywhere where it seems like you change immutable object, a new object is created instead.
So when you try to change immutable object inside list, a new object is created:
>>> l = [1] * 3
>>> id(l[0])
139685185487008
>>> id(l[1])
139685185487008
>>> id(l[2])
139685185487008
>>> l[0] = 2
>>> id(l[0])
139685185487040 # new object created instead of old object being modified
>>> id(l[1])
139685185487008
>>> id(l[2])
139685185487008