List Methods
copy()
Make a shallow copy of a list.
list.copy()
list.copy() creates a new list with the same elements as the original list.
Modifications to the copy (like appending or removing elements) do not affect the original list.
Returns
A new shallow copy of the list.
Try it
Run it and change it
original = [10, 20, 30]
clone = original.copy()
clone.append(40)
print("Original:", original)
print("Clone:", clone)Worth knowing
- Slice syntax equivalent:
clone = original[:]orlist(original)produces an identical shallow copy. - Deep copies: If the list contains nested lists or mutable objects, use
copy.deepcopy().