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()
The copy() method doesn't take any parameters.
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 |
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'}
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'}