Join Regular Classroom : Visit ClassroomTech

copy() method in Python List

It is a method in python that copies all the content of the list to a new list. It returns a new list, it doesn’t modify the old list. Any modification of the new list (copied list) will not affect the old list. This method doesn’t take any parameters in it.

Syntax:

new_list=old_list.copy()

Example 1:

s = [“Name“, “codewindow“, “Country“, “India“]
new = s.copy()
print(new)

Output:

[‘Name‘, ‘codewindow‘, ‘Country‘, ‘India‘]

Explanation:

It copied the existing list (s) to a new list (new) and returns it.

Example 2:

s = [“Name“,”codewindow“,”Country“,”India“]
new = s.copy()
new.append(“State“)
print(“new_list”,new)
print(“old_list”,s)

Output:

new_list [‘Name‘, ‘codewindow‘, ‘Country‘, ‘India‘, ‘State‘]
old_list [‘Name‘, ‘codewindow‘, ‘Country‘, ‘India‘]

Explanation:

Here, any changes to the new list did not affect the old list as u can clearly see.

You Missed
Also Checkout