["python","list","nested-lists"]
2D list has weird behavor when trying to modify a single value

When I try this code: data = [[None]*5]*5 data[0][0] = 'Cell A1' The value of data ends up like: [['Cell A1', None, None, None, None], ['Cell A1', None, None, None, None], ['Cell A1', None, None, None, None], ['Cell A1', None, None, None, None], ['Cell A1', None, None, None, None]] Why does the 'Cell A1' value appear in every nested list?
This makes a list with five references to the same list: data = [[None]*5]*5 Use something like this instead which creates five separate lists: >>> data = [[None]*5 for _ in range(5)] Now it behaves as expected: >>> data[0][0] = 'Cell A1' >>> print(data) [['Cell A1', None, None, None, None], [None, None, None, None, None], [None, None, None, None, None], [None, None, None, None, None], [None, None, None, None, None]]