Python List copy() method
Python List copy () method
The list.copy () method returns a shallow copy of the list.
Syntax
list.copy()
Parameter
NA
Return
This function returns the copy for the given list.
Example 1
# Python program explaining
# the list.copy() method
# passing the number List
oldList = [1,2,3,4,5,6,7,8,9,10,12]
print("Actual List: ",oldList)
# copying the the old list
newList= oldList.copy()
print("New List: ", newList)
Output
Actual List: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12] New List: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12]
Example 2
# Python program explaining
# the list.copy() method
# passing a mixed list
oldList = ['apple', 3, 16.2347]
# copying the list
new_list = oldList.copy()
# Adding element to the new list
new_list.append('orange')
# Printing the new and old list
print('Old List: ', oldList)
print('New List: ', new_list)
Output
Old List: ['apple', 3, 16.2347] New List: ['apple', 3, 16.2347, 'orange']
