Tutorial Study Image

Python copy()

The copy() function in python helps to make a copy of the set. We can say that it returns a shallow copy which means any changes in the new set won't reflect the original one.


set.copy() 
 

copy() Parameters:

The copy() method doesn't take any parameters.

copy() Return Value

Sometimes we use the =operator to copy the set the difference is that the '= 'operator creates the reference to the set and copy() creates a new set.

Input Return Value
set shallow copy

Examples of copy() method in Python

Example 1: How the copy() method works for sets in Python?


alphabet = {'a', 'b', 'c'}
new_set = alphabet .copy()

new_set.add(d)

print('Alphabets : ', alphabet )
print('New Set: ', new_set)
 

Output:


Alphabets:  {'a', 'b', 'c'}
New Set :  {'a', 'b', 'c', 'd'}

Example 2: How the copy() method works for sets using = operator in Python?


alphabet = {'a', 'b', 'c'}
new_set = alphabet

new_set.add(d)

print('Alphabets : ', alphabet )
print('New Set: ', new_set)
 

Output:


Alphabets:  {'a', 'b', 'c'}
New Set :  {'a', 'b', 'c', 'd'}