Possible Duplicate:
Two separate python lists acting as one
This code creates a dictionary of lists, the keys being in usens
. The lists are full of 0
s
usens = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 14, 110, 111, 112, 113, 114,
115, 116, 117, 118, 119, 302, 303, 306, 307, 308, 370, 371, 500,
501, 1000, 1010, 1020, 1100, 1200, 1201, 1202, 2000, 2001, 2002
]
empty = [[0]*length]*len(usens)
data = dict(zip(usens, empty))
pprint.pprint(data, indent=5)
data[0][0] += 1
pprint.pprint(data, indent=5)
The first pretty print returns the expected (blurb removed):
{0: [0, 0, 0, 0, 0],
1: [0, 0, 0, 0, 0],
2: [0, 0, 0, 0, 0],
3: [0, 0, 0, 0, 0],
4: [0, 0, 0, 0, 0],
5: [0, 0, 0, 0, 0],
.....
2001: [0, 0, 0, 0, 0],
2002: [0, 0, 0, 0, 0]}
I then do this: data[0][0] += 1
, expecting the first list (of index 0
) to begin with 1
, but nothing else to change.
However, all the lists have been modified:
{0: [1, 0, 0, 0, 0],
1: [1, 0, 0, 0, 0],
2: [1, 0, 0, 0, 0],
3: [1, 0, 0, 0, 0],
4: [1, 0, 0, 0, 0],
.....
2001: [1, 0, 0, 0, 0],
2002: [1, 0, 0, 0, 0]}
Why, and how can I make it so only one list is modified?