Python List copy() Method (with Examples)

Python List copy() Method: In this tutorial, we will learn about the copy() method of the list class with its usage, syntax, parameters, return type, and examples. By IncludeHelp Last updated : June 20, 2023

Python List copy() Method

The copy() is an inbuilt method of the list class that 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

The following is the syntax of copy() method:

list_name.copy()

Parameter(s):

The following are the parameter(s):

  • None - 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: Use of List copy() Method

# 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: Use of List copy() Method

# 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']

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.