Short answer : because dict objects are mutable and int objects are immutable.
Details:
See [{'a': True, 'b': True}] * 3
WITH
>>> l = [{}] * 3
you create a list containing 3 links to the same object.
>>> id(l[0]) 139685186829320 >>> id(l[1]) 139685186829320 >>> id(l[2]) 139685186829320
Therefore, when you change one of them, you change them all ( in the case of mutable objects ).
If you need a list of different dictionaries, you can do this 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 an immutable object. Wherever it seems that you are changing an immutable object, a new object is created instead.
Therefore, when you try to change an immutable object inside a 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