6.3. Iterable Set

  • Only unique values

  • Mutable - can add, remove, and modify items

  • Stores only hashable elements (int, float, bool, None, str, tuple)

  • Set is unordered data structure and do not record element position or insertion

  • Do not support getitem and slice

  • Contains in set has O(1) average case complexity [1]

6.3.1. Define Empty

  • data = set() - empty set

  • No short syntax

>>> data = set()

6.3.2. Define With Elements

  • data = {1, 2, 3} - set with values

  • Define With One Element

  • Define With Many Elements

  • Comma after last element of a one element set is optional

  • Brackets are required

Define With One Element:

>>> data = {1}
>>> data = {1,}

Define With Many Elements:

>>> data = {1, 2, 3}
>>> data = {1.1, 2.2, 3.3}
>>> data = {True, False}
>>> data = {'a', 'b', 'c'}
>>> data = {'a', 1, 2.2, True, None}

Comma after last element of a one element set is optional. Brackets are required

6.3.3. Unique Values

  • Sets store only unique values

  • Compares by values, not types

>>> data = {1, 2, 1}
>>>
>>> data
{1, 2}

Compares by values, not types:

>>> data = {1, 1.0}
>>> data
{1}
>>> data = {1.0, 1}
>>> data
{1.0}

6.3.4. Hashable

  • Can store elements of any hashable types

  • Hashable: int, float, bool, None, str, tuple

  • Non-hashable: list, set, dict

Hashable (Immutable):

  • int

  • float

  • bool

  • NoneType

  • str

  • tuple

>>> data = {'a', 1, 2.2, True, None}
>>> data = {1, 2.2, (3, 4)}

Non-hashable (Mutable):

  • list

  • set

  • dict

>>> data = {1, 2, [3, 4]}
Traceback (most recent call last):
TypeError: unhashable type: 'list'
>>>
>>> data = {1, 2, {3, 4}}
Traceback (most recent call last):
TypeError: unhashable type: 'set'

"Hashable types are also immutable" is true for builtin types, but it's not a universal truth.

  • More information in OOP Hash.

  • More information in OOP Identity.

6.3.5. Conversion

  • set() converts argument to set

>>> data = 'abcd'
>>> set(data) == {'a', 'b', 'c', 'd'}
True
>>> data = ['a', 'b', 'c', 'd']
>>> set(data) == {'a', 'b', 'c', 'd'}
True
>>> data = ('a', 'b', 'c', 'd')
>>> set(data) == {'a', 'b', 'c', 'd'}
True
>>> data = {'a', 'b', 'c', 'd'}
>>> set(data) == {'a', 'b', 'c', 'd'}
True

6.3.6. Deduplicate

Works with str, list, tuple

>>> data = [1, 2, 3, 1, 1, 2, 4]
>>> set(data)
{1, 2, 3, 4}

Converting set deduplicate items:

>>> data = ['Mark', 'Melissa', 'Rick', 'Mark']
>>>
>>> set(data) == {'Mark', 'Melissa', 'Rick'}
True

6.3.7. Add

  • set.add() - method

  • Adds element to set

  • Adds only unique values

  • Adds only hashable values

Adds element to set:

>>> data = {1, 2, 3}
>>> data.add(4)
>>>
>>> print(data)
{1, 2, 3, 4}

Adds only unique values:

>>> data = {1, 2, 3}
>>> data.add(3)
>>>
>>> print(data)
{1, 2, 3}

Adds only hashable values:

>>> data = {1, 2, 3}
>>> data.add((3, 4))
>>>
>>> print(data)
{1, 2, 3, (3, 4)}
>>> data = {1, 2, 3}
>>> data.add([3, 4])
Traceback (most recent call last):
TypeError: unhashable type: 'list'
>>> data = {1, 2, 3}
>>> data.add({3, 4})
Traceback (most recent call last):
TypeError: unhashable type: 'set'

6.3.8. Update

  • set.update() - method

  • Adds all elements from the sequence to the set

  • Adds only unique values

  • Adds only hashable values

>>> data = {1, 2}
>>> data.update({3, 4})
>>>
>>> data == {1, 2, 3, 4}
True
>>> data = {1, 2}
>>> data.update([3, 4])
>>>
>>> data == {1, 2, 3, 4}
True
>>> data = {1, 2}
>>> data.update((3, 4))
>>>
>>> data == {1, 2, 3, 4}
True

6.3.9. Pop

Gets and remove items

>>> data = {1, 2, 3}
>>> data.pop()
1

6.3.10. Membership

  • in - check if element is in set

>>> data = {1, 2, 3}
>>>
>>> 1 in data
True
>>> 0 in data
False

6.3.11. Union

  • Returns sum of elements from both sets

  • set.union() - method

  • | - operator

>>> a = {'mwatney', 'mlewis', 'rmartinez'}
>>> b = {'mwatney', 'avogel', 'bjohanssen'}

Union:

>>> a.union(b)  
{'mwatney', 'mlewis', 'rmartinez', 'bjohanssen', 'avogel'}
>>>
>>> a | b  
{'mwatney', 'mlewis', 'rmartinez', 'bjohanssen', 'avogel'}

Union update:

>>> a |= b
>>> a  
{'mwatney', 'mlewis', 'rmartinez', 'bjohanssen', 'avogel'}

6.3.12. Is Disjoint

  • Check if sets have common elements

  • Returns True or False

  • True - if sets don't have common elements

  • False - if sets have common elements

  • set.isdisjoint() - method

>>> a = {'mwatney', 'mlewis', 'rmartinez'}
>>> b = {'mwatney', 'avogel', 'bjohanssen'}
>>> a.isdisjoint(b)
False

6.3.13. Is Subset

  • Check if x has all elements from data

  • Returns True or False

  • True - if x has all elements from data

  • False - if x don't have element from data

  • set.issubset() - method

  • < - proper subset

  • <= - subset

>>> a = {'mwatney', 'mlewis', 'rmartinez'}
>>> b = {'mwatney', 'avogel', 'bjohanssen'}
>>> a.issubset(b)
False
>>> a < b
False
>>> a <= b
False

6.3.14. Is Superset

  • Check if data has all elements from x

  • Returns True or False

  • True - if data has all elements from x

  • False - if data don't have element from x

  • set.issuperset() - method

  • > - proper superset

  • >= - superset

>>> a = {'mwatney', 'mlewis', 'rmartinez'}
>>> b = {'mwatney', 'avogel', 'bjohanssen'}
>>> a.issuperset(b)
False
>>> a > b
False
>>> a >= b
False

6.3.15. Difference

  • Returns elements from data which are not in x

  • set.difference() - method

  • - - operator

>>> a = {'mwatney', 'mlewis', 'rmartinez'}
>>> b = {'mwatney', 'avogel', 'bjohanssen'}
>>> a.difference(b)  
{'rmartinez', 'mlewis'}
>>> a - b  
{'rmartinez', 'mlewis'}

6.3.16. Symmetric Difference

  • Returns elements from data and x, but without common elements

  • set.symmetric_difference() - method

  • ^ - operator

>>> a = {'mwatney', 'mlewis', 'rmartinez'}
>>> b = {'mwatney', 'avogel', 'bjohanssen'}
>>> a.symmetric_difference(b)  
{'mlewis', 'bjohanssen', 'rmartinez', 'avogel'}
>>> a ^ b  
{'mlewis', 'bjohanssen', 'rmartinez', 'avogel'}

6.3.17. Intersection

  • Returns common element from in data and x

  • set.intersection() - method

  • & - operator

>>> a = {'mwatney', 'mlewis', 'rmartinez'}
>>> b = {'mwatney', 'avogel', 'bjohanssen'}
>>> a.intersection(b)
{'mwatney'}
>>> a & b
{'mwatney'}

6.3.18. Cardinality

  • Number of elements in set

  • len() - function

>>> data = {1, 2, 3}
>>> len(data)
3

6.3.19. References

6.3.20. Assignments

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% About
# - Name: Iterable Set Definition
# - Difficulty: easy
# - Lines: 5
# - Minutes: 3

# %% English
# 1. Define `result_a` as a `set` without elements
# 2. Define `result_b` as a `set` with elements: 1, 2, 3
# 3. Define `result_c` as a `set` with elements: 1.1, 2.2, 3.3
# 4. Define `result_d` as a `set` with elements: 'a', 'b', 'c'
# 5. Define `result_e` as a `set` with elements: True, False, None
# 6. Define `result_f` as a `set` with elements: 1, 2.2, True, 'a'
# 7. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj `result_a` jako `set` bez elementów
# 2. Zdefiniuj `result_b` jako `set` z elementami: 1, 2, 3
# 3. Zdefiniuj `result_c` jako `set` z elementami: 1.1, 2.2, 3.3
# 4. Zdefiniuj `result_d` jako `set` z elementami: 'a', 'b', 'c'
# 5. Zdefiniuj `result_e` jako `set` z elementami: True, False, None
# 6. Zdefiniuj `result_f` jako `set` z elementami: 1, 2.2, True, 'a'
# 7. Uruchom doctesty - wszystkie muszą się powieść

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> assert result_a is not Ellipsis, \
'Assign your result to variable `result_a`'
>>> assert result_b is not Ellipsis, \
'Assign your result to variable `result_b`'
>>> assert result_c is not Ellipsis, \
'Assign your result to variable `result_c`'
>>> assert result_d is not Ellipsis, \
'Assign your result to variable `result_d`'
>>> assert result_e is not Ellipsis, \
'Assign your result to variable `result_e`'
>>> assert result_f is not Ellipsis, \
'Assign your result to variable `result_f`'

>>> assert type(result_a) is set, \
'Variable `result_a` has invalid type, should be set'
>>> assert type(result_b) is set, \
'Variable `result_b` has invalid type, should be set'
>>> assert type(result_c) is set, \
'Variable `result_c` has invalid type, should be set'
>>> assert type(result_d) is set, \
'Variable `result_d` has invalid type, should be set'
>>> assert type(result_e) is set, \
'Variable `result_e` has invalid type, should be set'
>>> assert type(result_f) is set, \
'Variable `result_f` has invalid type, should be set'

>>> assert result_a == set(), \
'Variable `result_a` has invalid value, should be set()'
>>> assert result_b == {1, 2, 3}, \
'Variable `result_b` has invalid value, should be {1, 2, 3}'
>>> assert result_c == {1.1, 2.2, 3.3}, \
'Variable `result_c` has invalid value, should be {1.1, 2.2, 3.3}'
>>> assert result_d == {'a', 'b', 'c'}, \
'Variable `result_d` has invalid value, should be {"a", "b", "c"}'
>>> assert result_e == {True, False, None}, \
'Variable `result_e` has invalid value, should be {True, False, None}'
>>> assert result_f == {1, 2.2, True, 'a'}, \
'Variable `result_f` has invalid value, should be {1, 2.2, True, "a"}'
"""

# Define `result_a` as a `set` without elements
# type: set
result_a = ...

# Define `result_b` as a `set` with elements: 1, 2, 3
# type: set[int]
result_b = ...

# Define `result_c` as a `set` with elements: 1.1, 2.2, 3.3
# type: set[float]
result_c = ...

# Define `result_d` as a `set` with elements: 'a', 'b', 'c'
# type: set[str]
result_d = ...

# Define `result_e` as a `set` with elements: True, False, None
# type: set[bool|None]
result_e = ...

# Define `result_f` as a `set` with elements: 1, 2.2, True, 'a'
# type: set[int|float|bool|str]
result_f = ...


# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% About
# - Name: Type Set Add
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2

# %% English
# 1. Insert character 'x' to the `result`
# 2. Run doctests - all must succeed

# %% Polish
# 1. Wstaw znak 'x' do `result`
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Hints
# - `set.add()`

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> assert result is not Ellipsis, \
'Assign your result to variable `result`'

>>> assert type(result) is set, \
'Variable `result` has invalid type, should be set'

>>> sorted(result)
['a', 'b', 'c', 'x']
"""

result = {'a', 'b', 'c'}


# Insert character 'x' to the `result`
# type: list[str]
...


# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% About
# - Name: Type Set Update
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2

# %% English
# 1. Insert characters from `data` to the `result`
# 2. Run doctests - all must succeed

# %% Polish
# 1. Wstaw znaki `data` do `result`
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Hints
# - `set.update()`

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> assert result is not Ellipsis, \
'Assign your result to variable `result`'

>>> assert type(result) is set, \
'Variable `result` has invalid type, should be set'

>>> sorted(result)
['a', 'b', 'c', 'x', 'y']
"""

data = {'x', 'y'}
result = {'a', 'b', 'c'}


# Insert characters from `data` to the `result`
# type: set[str]
...


# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% About
# - Name: Iterable Set Len
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2

# %% English
# 1. Define `result` with a result of checking length of `DATA`
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj `result` z wynikiem sprawdzenia długości `DATA`
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> assert result is not Ellipsis, \
'Assign your result to variable `result`'

>>> assert type(result) is int, \
'Variable `result` has invalid type, should be int'

>>> result
3
"""

DATA = {'a', 'b', 'c'}

# Define `result` with a result of checking length of `DATA`
# type: int
result = ...


# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% About
# - Name: Iterable Set Contains
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2

# %% English
# 1. Define `result` with a result of checking if 'x' is in the `DATA`
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj `result` z wynikiem sprawdzenia czy 'x' jest w `DATA`
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> assert result is not Ellipsis, \
'Assign your result to variable `result`'

>>> assert type(result) is bool, \
'Variable `result` has invalid type, should be bool'

>>> result
False
"""

DATA = {'a', 'b', 'c'}

# Define `result` with a result of checking if 'x' is in the `DATA`
# type: bool
result = ...