Python 3 reference · Set Methods
Python Set copy() Method
Return a shallow copy of the set: a new set with the same elements, which you can change without affecting the original.
Python 3 Since 3.0
Syntax
set.copy()
Return value
A new set.
Examples
a = {1, 2}
b = a.copy()
b.add(3)
print(sorted(a), sorted(b))
Output
[1, 2] [1, 2, 3]
a = {1, 2}
print(a.copy() == set(a), a.copy() is a)
Output
True False
Version history
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
frozenset()· Create an immutable set: unique items that can't be added or removed, so it is hashable and can be a dict key.- Python Sets · tutorial