copy() returns a new set that holds the same items as the original. The new set is a separate object, so adding or removing items in one does not affect the other.
The copy is shallow: the items themselves are not duplicated. That is rarely a problem for sets, because every item must be hashable, so you cannot change an item in place.
set.copy() is an instance method of the built-in set type: call it on a set with dot notation, such as team.copy(...). No import is needed.
Syntax
Call it with no arguments and keep the returned set in a new variable.
python
duplicate = s.copy()
Call
What it does
{1, 2, 3}.copy()
Returns {1, 2, 3}, a brand-new set with the same items.
set().copy()
Returns set(): copying an empty set gives another empty set.
{1}.copy(2)
Raises TypeError: copy() takes no arguments.
set(s) and s | set() also make copies, and frozenset(s) makes an unchangeable one. A plain b = a does not copy anything: both names point to the same set.
Method signatures
python
set.copy()
# returns: a new set with the same items (a shallow copy)
Parameters
This method takes no parameters: copy(x) raises TypeError.
Return Value
Return Value
Description
set
A new, independent set with the same items as the original, in the same type.
Python Examples
Example 1 — Copy a Set Before Changing It
Compare a plain alias with a real copy after the original grows.
python
original = {"red", "green"}
alias = original
duplicate = original.copy()
original.add("blue")
print(sorted(alias))
print(sorted(duplicate))
print(alias is original, duplicate is original)
An alias shares the set.alias = original makes a second name, so adding "blue" shows up through both names.
A copy is separate.duplicate was copied before the change, so it still holds only green and red.
Identity tells the story.alias is original is True, while the copy is a different object, so duplicate is original is False.
Example 2 — Try Three Ways to Copy a Set
Use copy(), the set() constructor, and an empty union, and compare the results.
python
base = {1, 2, 3}
a = base.copy()
b = set(base)
c = base | set()
a.add(10)
print(sorted(base), sorted(a))
print(a == b, b == c, b is c)
print(len(base), len(b), len(c))
Output
[1, 2, 3] [1, 2, 3, 10]
False True False
3 3 3
How It Works
Each way makes a new set.copy(), set(base), and base | set() all return a fresh set with the same items.
Changing one copy. Adding 10 to a does not touch base, which still has 1, 2, and 3.
Equal, but not the same.b == c is True because the items match, while b is c is False because they are different objects.
Sizes. The base set and the two untouched copies each have three items.
Example 3 — Try a Change on a Copy and Keep the Original
Edit a copy of a permission set, then compare it with the original.