Python Trap
Python List Copy Trap: Why b = a Doesn't Make a Copy
· Python list copy trap: b = a is not a copy | a ko kisne badla?
In Python, b = a doesn't copy a list. It gives the same list a second name, so b.append(4) changes a to [1, 2, 3, 4] too. Use a.copy() for a separate list. It is a shallow copy, so nested lists need copy.deepcopy.
The code
a = [1, 2, 3]
b = a
b.append(4)
print(a)b = a.copy()![Animated Python explainer: def add(item, bag=[]) is called twice and prints [1], then [1, 2], because both calls reuse the same default list. The fix sets bag=None and creates a new list.](https://i.ytimg.com/vi/NuXuvThqEuM/maxresdefault.jpg)


