Python

copy() method in list

Click for All Topics

copy() method in list - Python

  • This method is used to create a shallow copy of a list.
  • The .copy() method doesn’t take any argument.
  • It will return a copy of a list.
  • When we call this method with a list it will create a new list in the backend and iterate over the element of the original list and every element will be appended to the new list, finally it will return a new list.
  • It returns a shallow copy of a list which means when we modify the original list it will not affect the copied list and vice versa.
  • The time complexity of this method is O(n) where n is the number of element in original list

 

Syntax –

list.copy()

 

Parameters –

 

  • argument → Doesn’t take any argument
  • return → return a shallow copy of a list

 

Example –

				
					my_list=['hello', 'students', 'warm', 'welcome']
print("Original list:",my_list)
copied_list=my_list.copy()
print("Copied list:",copied_list)

# Output -
# Original list: ['hello', 'students', 'warm', 'welcome']
# Copied list: ['hello', 'students', 'warm', 'welcome']
				
			
  • In the above example, first we create a list with some elements and store it in my_list variable.
  • After creating my_list print the original list on the output screen.
  • Then create a copy of the original list using the .copy() method and store the copied list in the copied_list variable.
  • Finally, print the copied list on the output screen.

 

Example 2- Let’s see if modifying the original list affects the copied list or not.

				
					chambal=['bhind','morena','sheopur']
print("Orignal list:",chambal)
chambal_copy=chambal.copy()
print("Copied List:",chambal_copy)
chambal.append('gwalior')
print("Modify original list:",chambal)
print("Copied list:",chambal_copy)

# Ouput -
# Orignal list: ['bhind', 'morena', 'sheopur']
# Copied List: ['bhind', 'morena', 'sheopur']
# Modify original list: ['bhind', 'morena', 'sheopur', 'gwalior']
# Copied list: ['bhind', 'morena', 'sheopur']
				
			
  • Example 3- create a copy of a list without using an in-built method e.g. .copy()
				
					my_list=['hello', 'student', 'this', 'is', 'edSlash']
new_list=my_list[:]
print("Original list:",my_list)
print("Copied list:",new_list)

# Output -
# Original list: ['hello', 'student', 'this', 'is', 'edSlash']
# Copied list: ['hello', 'student', 'this', 'is', 'edSlash']
				
			

Example 4-

				
					lst=[10,20,30,40,50]
lst1=[]
for i in lst:
	lst1.append(i)
print("Original list:",lst)
print("Modified list:",lst1)

# Output -
# Original list: [10, 20, 30, 40, 50]
# Modified list: [10, 20, 30, 40, 50]
				
			

Example 5-

				
					my_list=[2,4,6,8,10,12,14,16]
my_list1=my_list
print("Original list:",my_list)
print("Copied list:"my_list1)
my_list.append(18)
print("\nModified original list:",my_list)
print("Copied list:",my_list1)

# Output -
# Original list: [2, 4, 6, 8, 10, 12, 14, 16]
# Copied list: [2, 4, 6, 8, 10, 12, 14, 16]

# Modified original list: [2, 4, 6, 8, 10, 12, 14, 16, 18]
# Copied list: [2, 4, 6, 8, 10, 12, 14, 16, 18]
				
			
  • In the above example, you can see that when we modify the original list it modifies the copied list as well.
  • It is because we do not create a copy of the original list instead of creating a copy we create another reference of the original list object.
  • In this situation, if we modify any list the changes will affect both the lists.