A mutable collection of unique values. CogniTab keeps iteration order stable so script output is easy to read.
Syntax
set(iterable=None, /)Returns
Example
seen = {1, 2, 2}
seen.add(3)
if 2 in seen:
text(str(seen))A mutable collection of unique values. CogniTab keeps iteration order stable so script output is easy to read.
set(iterable=None, /)seen = {1, 2, 2}
seen.add(3)
if 2 in seen:
text(str(seen))Create an empty set or collect unique values from another iterable, including a generator expression.
set()
set(iterable, /)| Name | Description |
|---|---|
iterableIterable | Optional source values to deduplicate into the new set. Depends on overload · Positional only Default: None |
seen = set()
unique = set([1, 2, 2, 3])Create a set literal inline. Use dict() or {} for an empty dictionary; use set() for an empty set.
{1, 2, 3}| Name | Description |
|---|---|
valueAny? | Each value written inside the braces. Required · Positional or named No default |
unique = {1, 2, 2, 3}
text(str(unique)) # returns {1, 2, 3}Build a set by looping through an iterable and keeping unique calculated values. Multiple for/if clauses are supported.
{expr for target in iterable if condition}| Name | Description |
|---|---|
exprAny? | Value calculated for each accepted item. Required · Positional or named No default |
targetName | destructuring target | Loop target for the current item. Starred targets are not supported. Required · Positional or named No default |
iterableIterable | Source values to loop through. Required · Positional or named No default |
conditionBoolean | Optional filter checked before adding a value. Optional · Positional only Default: True |
values = [1, 2, 3, 4, 5]
remainders = {x % 3 for x in values}Count how many unique values are in the set.
len(set, /)Int — Number of set values.values = {1, 2, 2}
count = len(values) # returns 2Check whether a value exists in the set.
value in set| Name | Description |
|---|---|
valueAny? | Value to test. Required · Positional or named No default |
setset | Set used for membership. Required · Positional or named No default |
Boolean — true when the value exists, otherwise false.values = {1, 2, 3}
if 2 in values:
text("ready")Add one value to the set. Existing values are kept once.
set.add(value, /)| Name | Description |
|---|---|
valueAny? | Value to add. Required · Positional only No default |
None — No direct return value. The set is updated in place.seen = set()
seen.add("home")Remove a value if it exists. Missing values are ignored.
set.discard(value, /)| Name | Description |
|---|---|
valueAny? | Value to remove if present. Required · Positional only No default |
None — No direct return value. The set may be updated in place.seen = {"home", "shop"}
seen.discard("shop")Remove an existing value. Missing values raise a runtime error so mistakes are visible.
set.remove(value, /)| Name | Description |
|---|---|
valueAny? | Value that must exist in the set. Required · Positional only No default |
None — No direct return value. The set is updated in place.seen = {"home", "shop"}
seen.remove("home")Remove and return one value from the set. Empty sets raise a runtime error.
set.pop()Any? — Removed value.seen = {"home", "shop"}
value = seen.pop()Add values from one or more iterables into the set.
set.update(*iterables)| Name | Description |
|---|---|
iterablesIterable | Sources to add. Optional · Variadic No default |
None — No direct return value. The set is updated in place.seen = {"home"}
seen.update(["shop", "home"])Return a new set with values from this set and the provided iterables.
set.union(*iterables)| Name | Description |
|---|---|
iterablesIterable | Sources to merge. Optional · Variadic No default |
a = {"home"}
b = a.union(["shop"])Return a new set containing values that also exist in every provided iterable.
set.intersection(*iterables)| Name | Description |
|---|---|
iterablesIterable | Sources to compare. Optional · Variadic No default |
a = {"home", "shop"}
b = a.intersection(["shop"])Return a new set without values found in the provided iterables.
set.difference(*iterables)| Name | Description |
|---|---|
iterablesIterable | Sources to subtract. Optional · Variadic No default |
a = {"home", "shop"}
b = a.difference(["home"])Copy the set into a new mutable set.
set.copy()seen = {1, 2}
copied = seen.copy()Remove every value from the set.
set.clear()None — No direct return value. The set becomes empty.seen = {1, 2}
seen.clear()Iterate values in stable insertion order.
for value in set:None — No direct return value. The loop runs once per value.values = {"home", "shop"}
for value in values:
text(value)Check whether the set has at least one value. An empty set is treated as false.
if set | if not setBoolean condition — true when the set is not empty, false when it is empty.seen = set()
if not seen:
text("empty")