Click for All Topics
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}
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'}
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
# 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'}
Office:- 660, Sector 14A, Vasundhara, Ghaziabad, Uttar Pradesh - 201012, India