Home »
Python
Python List copy() Method with Example
Python List copy() Method: Here, we are going to learn how to copy a list in Python?
Submitted by IncludeHelp, on December 03, 2019
List copy() Method
copy() method is used to copy a list, the method is called with this list (current/original list) and returns a list of the same elements.
Syntax:
list_name.copy()
Parameter(s):
- It does not accept any parameter.
Return value:
The return type of this method is <class 'list'>, it returns a list containing the all elements.
Example 1:
# Python List copy() Method with Example
# declaring the list
cars = ["Porsche", "Audi", "Lexus"]
# copying the list
x = cars.copy()
# printing the lists
print("cars: ", cars)
print("x: ", x)
Output
cars: ['Porsche', 'Audi', 'Lexus']
x: ['Porsche', 'Audi', 'Lexus']
Example 2:
# Python List copy() Method with Example
# declaring the lists
x = ["ABC", "XYZ", "PQR"]
y = ["PQR", "MNO", "YXZ"]
z = ["123", "456", "789"]
# printing the copies of the lists
print("copy of x: ", x.copy())
print("copy of y: ", y.copy())
print("copy of z: ", z.copy())
Output
copy of x: ['ABC', 'XYZ', 'PQR']
copy of y: ['PQR', 'MNO', 'YXZ']
copy of z: ['123', '456', '789']
ADVERTISEMENT
ADVERTISEMENT