set

Store values without duplicates and add or remove them later. Equal values appear only once: True, 1 and 1.0 are equal. Items must be usable as dictionary keys; a list, dictionary, set or tuple containing one of them is rejected. Do not rely on iteration order.

Syntax

set(iterable=(), /)

Returns

Example

seen = {1, 2, 2}
seen.add(3)

if 2 in seen:
    text(str(seen))

Methods

17 methods

set.constructor

Create an empty set or collect values from a source you can loop through, such as a list or generator, keeping only the first occurrence of each equal value.

Syntax

set()
set(iterable, /)

Parameters

NameDescription
iterableIterable
Optional source values to deduplicate into the new set.
Depends on overload · Positional only
Default: ()

Returns

set — A new mutable set.

Example

seen = set()
unique = set([1, 2, 2, 3])

set.literal

Create a set by writing values between braces. Use set() for an empty set; {} creates an empty dictionary.

Syntax

{1, 2, 3}

Parameters

NameDescription
valueAny?
Each value written inside the braces.
Required · Positional or named
No default

Returns

set — A new mutable set that keeps one copy of each value.

Example

unique = {1, 2, 2, 3}
text(str(unique))        # returns {1, 2, 3}

set.comprehension

Build a set from values produced by expr while looping through a source, removing duplicates. Use if to filter items; multiple for/if clauses are allowed. Loop variables stay inside this expression.

Syntax

{expr for target in iterable if condition}

Parameters

NameDescription
exprAny?
Value calculated for each accepted item.
Required · Positional or named
No default
targetName | destructuring target
Variable or group of variables receiving the current item. A target such as *rest is not supported.
Required · Positional or named
No default
iterableIterable
Source you can loop through, such as a string, collection or generator; a dictionary yields its keys.
Required · Positional or named
No default
conditionBoolean
Optional filter checked before adding a value.
Optional · Positional only
Default: True

Returns

set — A new mutable set.

Example

values = [1, 2, 3, 4, 5]
remainders = {x % 3 for x in values}

set.length

Count how many unique values are in the set.

Syntax

len(set, /)

Parameters

NameDescription
setset
The set to count.
Required · Positional only
No default

Returns

Int — Number of set values.

Example

values = {1, 2, 2}
count = len(values)      # returns 2

set.membership_operator

Check whether a value exists in the set.

Syntax

value in set

Parameters

NameDescription
valueAny?
Value to test.
Required · Positional or named
No default
setset
Set to search for the value.
Required · Positional or named
No default

Returns

Boolean — true when the value exists, otherwise false.

Example

values = {1, 2, 3}
if 2 in values:
    text("ready")

set.add_method

Add one value to the set. Existing values are kept once.

Syntax

set.add(value, /)

Parameters

NameDescription
valueAny?
Value to add.
Required · Positional only
No default

Returns

None — No direct return value. The set is updated in place.

Example

seen = set()
seen.add("home")

set.discard_method

Remove a value if it exists. Missing values are ignored.

Syntax

set.discard(value, /)

Parameters

NameDescription
valueAny?
Value to remove if present.
Required · Positional only
No default

Returns

None — No direct return value. The set may be updated in place.

Example

seen = {"home", "shop"}
seen.discard("shop")

set.remove_method

Remove a value from this set, or raise KeyError if it is absent.

Syntax

set.remove(value, /)

Parameters

NameDescription
valueAny?
Value that must exist in the set.
Required · Positional only
No default

Returns

None — No direct return value. The set is updated in place.

Example

seen = {"home", "shop"}
seen.remove("home")

set.pop_method

Remove and return an arbitrary value from this set. An empty set raises KeyError.

Syntax

set.pop()

Returns

Any? — Removed value.

Example

seen = {"home", "shop"}
value = seen.pop()

set.update_method

Add values from sources you can loop through to this set, keeping each equal value only once.

Syntax

set.update(*iterables)

Parameters

NameDescription
iterablesIterable
Sources to add.
Optional · Variadic
No default

Returns

None — No direct return value. The set is updated in place.

Example

seen = {"home"}
seen.update(["shop", "home"])

set.union_method

Return a new set combining this set with sources you can loop through, without duplicates. This set is unchanged.

Syntax

set.union(*iterables)

Parameters

NameDescription
iterablesIterable
Sources to merge.
Optional · Variadic
No default

Returns

set — Merged set copy.

Example

a = {"home"}
b = a.union(["shop"])

set.intersection_method

Return a new set containing values from this set that appear in every supplied source. With no sources, return a copy. This set is unchanged.

Syntax

set.intersection(*iterables)

Parameters

NameDescription
iterablesIterable
Sources to compare.
Optional · Variadic
No default

Returns

set — Intersection set copy.

Example

a = {"home", "shop"}
b = a.intersection(["shop"])

set.difference_method

Return a new set containing values from this set that are absent from all supplied sources. With no sources, return a copy. This set is unchanged.

Syntax

set.difference(*iterables)

Parameters

NameDescription
iterablesIterable
Sources to subtract.
Optional · Variadic
No default

Returns

set — Difference set copy.

Example

a = {"home", "shop"}
b = a.difference(["home"])

set.copy_method

Return a new set with the same values. Adding or removing values in the copy does not change the original.

Syntax

set.copy()

Returns

set — A new set with the same values.

Example

seen = {1, 2}
copied = seen.copy()

set.clear_method

Remove every value from the set.

Syntax

set.clear()

Returns

None — No direct return value. The set becomes empty.

Example

seen = {1, 2}
seen.clear()

set.for_loop

Loop through each value once. Do not rely on a particular order.

Syntax

for value in set:

Parameters

NameDescription
valueAny?
Current set value in the loop.
Required · Positional or named
No default
setset
set to iterate.
Required · Positional or named
No default

Returns

None — No direct return value. The loop runs once per value.

Example

values = {"home", "shop"}
for value in values:
    text(value)

set.truthy_check

Check whether the set has at least one value. An empty set is treated as false.

Syntax

if set | if not set

Parameters

NameDescription
setset
The set used in the condition.
Required · Positional or named
No default

Returns

Boolean condition — true when the set is not empty, false when it is empty.

Example

seen = set()
if not seen:
    text("empty")