What is the best way to copy a list? [duplicate]

If you want a shallow copy (elements aren’t copied) use:

lst2=lst1[:]

If you want to make a deep copy then use the copy module:

import copy
lst2=copy.deepcopy(lst1)

Leave a Comment