Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

set()

Collects distinct hashable values into a mutable set.

Version: Python 3.14; repository image, production runtime not attested

Syntax

set([iterable], /)

Collects distinct hashable values into a mutable set.

Equality determines duplicates. Equal integer and Boolean inputs represent the same set member.

The constructor inserts hashable input elements into a mutable set. The two occurrences of 3 collapse; sorted is used only to display 1 and 3 in stable order. The values 1, True and 1.0 compare equal and share a hash, so the set has one member. Repeating the same object also keeps one representative.

x = float("nan")
assert len(set([x, x])) == 1 and len(set([x, float("nan")])) == 2
assert len(set([1, True, 1.0])) == 1

Examples

print(sorted(set([3, 1, 3])))
print(len(set([1, True, 1.0])))

Result

[1, 3]
1

The constructor inserts hashable input elements into a mutable set. The two occurrences of 3 collapse; sorted is used only to display 1 and 3 in stable order. The values 1, True and 1.0 compare equal and share a hash, so the set has one member. Repeating the same object also keeps one representative.

Sources