Skip to content Skip to sidebar Skip to footer

Python Nested List Unexpected Behaviour

I've ran into an unexpected behavior when using a nested list in python, that took a while to debug. If a list is initialized like this: a = [[None] * 2] * 2 a [[None, None], [None

Solution 1:

>>> a = [[None] * 2] * 2
>>> id(a[0])
41554168
>>> id(a[1])
41554168
>>> b = [[None, None], [None, None]]
>>> id(b[0])
41549576
>>> id(b[1])
41557368

This should explain


Post a Comment for "Python Nested List Unexpected Behaviour"