Python

Copy method in set

Click for All Topics

Copy method in set - Python

  • This method is used to create a copy of a set.
  • It creates a shallow copy of the set which means when we modify the original set it will not affect the copied set.
  • this method iterates through each element of the original set and adds reference to the copied set.
  • The copy method doesn’t take any argument.
  • The return value of the copy method is a shallow copy of the original object(set).
  • We can also create a copy of a set using the assignment operator(=) but there is a disadvantage with this method.
  • When we create a copy using the assignment operator then when we modify the original set it will affect the copied set.

 

Syntax – let’s see the syntax

original_set.copy()

 

Example1 – let’s see an example

				
					original_set={1,2,3,4,5}
print("Original set:",original_set)
copied_set=original_set.copy()
print("Copied set:",copied_set)

# Output -
# Original set: {1, 2, 3, 4, 5}
# Copied set: {1, 2, 3, 4, 5}
				
			
  • In the above example first, we create a set original_set with elements 1,2,3,4,5.
  • After creating original_set print the original_set on the output screen.
  • Then we create a copy of the original_set using the copy method and store the copy in the variable copied_set.
  • Then print the copied set on the output screen.

 

Example 2-

				
					s={'hello','this', 'is', 'edSlash'}
print("original set:",s)
s1=s.copy()
print("Copied set:",s1)
s.remove('hello')
print("Copied set after modifying original set:",s1)
print("Modified original set:",s)

# Output -
# original set: {'hello', 'this', 'is', 'edSlash'}
# Copied set: {'hello', 'this', 'is', 'edSlash'}
# Copied set after modifying original set: {'hello', 'this', 'is', 'edSlash'}
# Modified original set: {'this', 'is', 'edSlash'}
				
			
Create a copy of the set using the assignment operator(=) –

Example 3-

				
					s={'a','b','c','d','e','f'}
s1=s
print(s)
print(s1)
s.add('g')
print(s)
print(s1)

# Output - 
# original set: {'f', 'd', 'b', 'a', 'c', 'e'}
# copied set: {'f', 'd', 'b', 'a', 'c', 'e'}
# Modified original set: {'f', 'd', 'g', 'b', 'a', 'c', 'e'}
# Copied set: {'f', 'd', 'g', 'b', 'a', 'c', 'e'}
				
			

In the above example, we can see that when we modify the original set the changes affect both the set.

this is because we can’t create a new set Instead of creating a new set we create a reference of variables that store the original set that’s why changes affect both sets.

Example 4- Let’s see what happens when we try to give an argument to copy method

				
					s={11,111,1111,11111,111111}
print(s)
s.copy(11)

# Output -
# {111111, 1111, 11111, 11, 111}
# Traceback (most recent call last):
  # File "/home/main.py", line 3, in <module>
    # s.copy(11)
# TypeError: set.copy() takes no arguments (1 given)
				
			

Example 5-

				
					s={'CSE','IT','EC','EE','ME'}
s1=s2=s.copy()
print("original set:",s)
print("Copied set 1:",s1)
print("Copied set 2:",s2)

# Output -
# original set: {'EE', 'EC', 'CSE', 'ME', 'IT'}
# Copied set 1: {'EE', 'EC', 'CSE', 'ME', 'IT'}
# Copied set 2: {'EE', 'EC', 'CSE', 'ME', 'IT'}