Saturday, January 9, 2021

How To Copy List In Python

 To copy an existing list to another in Python, we can use copy () function and the function is invoked on the existing list object. This function returns a new object in which the elements of the existing list is copied.

Copy a List

mylist = ["assam", "banaras", "chennai"]

clist = mylist.copy()


Another way to make a copy is to use the built-in method list().

Example

mylist = ["assam", "banaras", "chennai"]

clist = list(mylist)


No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner