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

Copy before changingTry it yourself
a = {1, 2}
b = a.copy()
b.add(3)
print(sorted(a), sorted(b))

Output

[1, 2] [1, 2, 3]
Equal but a different objectTry it yourself
a = {1, 2}
print(a.copy() == set(a), a.copy() is a)

Output

True False

Version history

VersionChange
Python 3.0Added