Multiple ways to Clone or Copy a list in Python

Pallavi Kumari - Jul 9 - - Dev Community

Python provide multiple ways to achieve the desired result. For example, to copy a list to another list there are different methods:

  • Through List slicing operation

#Method 1
lstcopy = lst[:]
print("Cloning By list slicing lst[:] ",lstcopy)

  • Through copy() method

#Method2
lstcopy = lst.copy()
print("Cloning By list copying lst.copy() ",lstcopy)

  • List Comprehension operation

#Method3
lstcopy = [i for i in lst]
print("Cloning By list Comprehension ",lstcopy)

  • Through append() method

#Method4
lstcopy = []
for i in lst :
lstcopy.append(i)
print("Cloning By list append ",lstcopy)

Thanks,

I hope you learn something new from this article! Thanks for your support

. .