List copy() method Python

The copy() method of list does a shallow copy of the elements of the list object.

>>> ls=[23 , 67 ,'Test' , 'New' , 90 , (90,) , [45]]
>>> l=ls.copy()
>>> l
[23, 67, 'Test', 'New', 90, (90,), [45]]

Point to note

Calling copy() method is same as using
 
l[:]
 
to copy the elements of the list object.

>>> l=[23 ,90.56 , 5859.575 , (90,) ]
>>> ls=l[:]
>>> ls
[23, 90.56, 5859.575, (90,)]


Leave a Reply

Your email address will not be published. Required fields are marked *