Python Function Reference

set.copy

Create a shallow copy of the set.

Python 3.13
set.copy()

Return value

A new set with the same items.

Examples

The copy changes separately from the original

Python 3.13
s1 = {1, 2}
s2 = s1.copy()
s2.add(3)
print(s1, s2)
{1, 2} {1, 2, 3}

Assignment does not create a copy

Python 3.13
s1 = {1, 2}
s2 = s1
s2.add(3)
print(s1)
{1, 2, 3}

Details

set(s) does the same. Assignment s2 = s1 does not create a copy: both variables refer to the same set, and changes are visible through both.

See also