Python set.copy() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
set method

Definition

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()
CallWhat 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 ValueDescription
setA 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)

How It Works

  1. An alias shares the set. alias = original makes a second name, so adding "blue" shows up through both names.
  2. A copy is separate. duplicate was copied before the change, so it still holds only green and red.
  3. 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))

How It Works

  1. Each way makes a new set. copy(), set(base), and base | set() all return a fresh set with the same items.
  2. Changing one copy. Adding 10 to a does not touch base, which still has 1, 2, and 3.
  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.
  4. 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.

python
allowed = {"read", "write"}
trial = allowed.copy()
trial.discard("write")
trial.add("admin")

print(sorted(allowed))
print(sorted(trial))
print(trial.issubset(allowed))

How It Works

  1. Experiment on the copy. The trial set loses "write" and gains "admin", while the original is never touched.
  2. The original is safe. allowed still has read and write after the edits.
  3. Compare the two. "admin" is not in the original permissions, so the trial set is not a subset of it and the last line prints False.

Frequently Asked Questions

It returns a new set with the same items. The new set is a separate object, so changing one does not change the other.
b = a makes a second name for the same set, so both change together. b = a.copy() makes an independent set.
A shallow copy, but that is almost always enough, because set items must be hashable and cannot be changed in place. You rarely need copy.deepcopy().
set(s) and s | set() also work, and frozenset(s) makes an unchangeable copy. Slicing does not work, because a set has no positions.
Changing a set while looping over it raises RuntimeError. Loop over a copy or list(s) and change the original set instead.

Next: set.difference()

Return a new set with the items of the set that are not in any other iterable.

difference() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful