Python list copy() method
Return a shallow copy of a list.
Syntax
list.copy()
Assignment (b = a) never copies: both names point to one list. copy() makes a new list. It is "shallow": lists inside it are still shared; use copy.deepcopy for those.
Parameters
None.
Return value
a new list
Example
Code
a = [1, 2, 3] b = a c = a.copy() b.append(4) print(a, c) grid = [[0, 0], [0, 0]] g2 = grid.copy() g2[0][0] = 9 print(grid) # the inner list is shared
Output
[1, 2, 3, 4] [1, 2, 3] [[9, 0], [0, 0]]
Related
Learn it step by step in the free lesson Lists & Tuples, with exercises that check your answers.