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






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.