all Python 3Sets (set)function
set.copy
copying
Return a shallow copy of the set (a new independent set).
Syntax
s.copy()
Plain assignment is not a copy: both names refer to the same set, so adding through one is visible through the other, and only copy() gives an independent object. The shallowness rarely bites here, since set elements must be hashable — in practice numbers, strings and tuples; the only objects still shared between the copies are custom classes with their own hash.
Examples
a = {1, 2}
b = a.copy()
b.add(3)
print(sorted(a)) # → [1, 2]
print(sorted(b)) # → [1, 2, 3]
See also
Open in the full glossarydocs.python.orgReport an inaccuracy