Python copy List is one of the List methods used to shallow copy a Lists. I mean, copying list into a new list. The syntax of the list copy function is
list_name.copy()
Python List copy Function Example
The copy function copies total items in a given list to a new list. The below code copy list a to newList.
# Python Copy List Function a = [10, 20, 30, 40] print("Original List : ", a) newList = a.copy() print("Copied List : ", newList)
In this list copy example, we declared a string list. Next, we used the copy function on it.
TIP: Please refer to List and List methods articles in Python.
# Python Copy List Function strFruits = ['Apple', 'Banana', 'Kiwi', 'Grape'] print("Original List : ", strFruits) newList = strFruits.copy() print("Copied List : ", newList)
copy List Function Example 2
Let me use this list copy function on Mixed List.
# Python Copy List Function MixList = ['apple', 1, 'Banana', 5, 'Kiwi', 'Mango'] print("Original List : ", MixList) newList = MixList.copy() print("Copied List : ", newList)
List copy Function Example 3
In this program, we used List copy function on the Nested list (list inside a list).
# Python Copy List Function MixList = [[71, 222], [222, 13], [14, 15], [99, 77]] print("Original List : ", MixList) newList = MixList.copy() print("Copied List : ", newList)