Python Forum

Full Version: why does the list of list replicate this way?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.


a = [[]] * 10

a
[[], [], [], [], [], [], [], [], [], []]

a[0].append((1,2))

a
[[(1, 2)], [(1, 2)], [(1, 2)], [(1, 2)], [(1, 2)], [(1, 2)], [(1, 2)], [(1, 2)], [(1, 2)], [(1, 2)]]
Why does it replicate the values across the entire list? Bizarre Huh
Because list are reference. [[]] * 10 makes 10 reference copies.
a = [[] for x in range(10)]
Thanks very much windspar.
That was as simple as that :)