Python traps · Python
Python: I changed b... and a changed?!PRO
b = a gives the same list a second name. a.copy() makes a new list.
# I changed b... and a changed ?! # b = a does NOT copy the list: both names point at the SAME list. a = [1, 2] b = a b.append(3) print(a) # [1, 2, 3] ?! print(a is b) # True: one list, two names # Fix: make a real copy a = [1, 2] b = a.copy() b.append(3) print(a) # [1, 2] ✓Open code P10 in Pocket Compiler Download the Pocket Compiler app — free
No app yet? Install it, then open the app and type the code P10 in Code Library.