4.3. Sequence set

  • Only unique values

  • Mutable - can add, remove, and modify items

  • Can store elements of any hashable types

4.3.1. Type Definition

  • Do not record element position

  • Do not record order of insertion

  • Do not support indexing

  • Do not support slicing

  • Defining only with set() - no short syntax

  • Comma after last element is optional

  • Brackets are required

Listing 51. Initialize empty
my_set = set()
Listing 52. Initialize with one element
my_set = {1}
my_set = {1,}
Listing 53. Initialize with many elements. Stores only unique values.
my_set = {1, 3, 1}
# {1, 3}

my_set = {1, 2.0, 'Jan'}
# {1, 2.0, 'Jan'}

my_set = {1, 2.0, (3, 4)}
# {1, 2.0, (3, 4)}

my_set = {1, 2.0, [3, 4]}
# TypeError: unhashable type: 'list'

my_set = {1, 2.0, {3, 4}}
# TypeError: unhashable type: 'set'
{1}
# {1}

{1.0}
# {1.0}

{1, 1.0}
# {1}

{1.0, 1}
# {1.0}

4.3.2. Type Annotation

my_set: set = set()
from typing import Set

my_set: Set[int] = {1, 2, 3}
my_set: Set[float] = {0.0, 1.1, 2.2}
my_set: Set[str] = {'a', 'b', 'c'}

4.3.3. Adding Items

4.3.3.1. Adding Single Item

my_set = {1, 2}

my_set.add(3)
# {1, 2, 3}

my_set.add(3)
# {1, 2, 3}

my_set.add(4)
# {1, 2, 3, 4}

4.3.3.2. Adding Many Items

my_set = {1, 2}
# {1, 2}

my_set.update({3, 4})
# {1, 2, 3, 4}

my_set.update([5, 6])
# {1, 2, 3, 4, 5, 6}

my_set.update((7, 8))
# {1, 2, 3, 4, 5, 6, 7, 8}

4.3.4. Popping Items

  • Gets and remove items

my_set = {1, 2, 3}

value = my_set.pop()

print(my_set)
# {1, 2}

print(value)
# 3

4.3.5. Deduplicate Items

Listing 54. Converting list to set deduplicate items
names = [
    'Twardowski',
    'Twardowski',
    'Jiménez',
    'Twardowski'
]

unique_names = set(names)
# {'Twardowski', 'Jiménez'}
Listing 55. Converting tuple to set deduplicate items
names = (
    'Twardowski',
    'Twardowski',
    'Jiménez',
    'Twardowski'
)

unique_names = set(names)
# {'Twardowski', 'Jiménez'}

4.3.6. Membership Operators

Listing 56. Equals and Not-equals
{1, 2} == {1, 2}          # True
{1, 2} == {2, 1}          # True

{1, 2} != {1, 2}          # False
{1, 2, 3} != {1, 2}       # True
Listing 57. Contains
1 in {1, 2}               # True
2 in {1, 2}               # True
3 in {1, 2}               # False

{1} in {1, 2}             # False
{2} in {1, 2}             # False
{3} in {1, 2}             # False

{1,} in {1, 2}            # False
{2,} in {1, 2}            # False
{3,} in {1, 2}            # False

{1, 2} in {1, 2}          # False
{3, 4} in {1,2, {3, 4}}   # True
Listing 58. Missing
1 not in {1, 2}           # False
3 not in {1, 2}           # True

{2} not in {1, 2}         # True
{1, 2} not in {1, 2}      # True

4.3.7. Membership

4.3.7.1. Disjoint

  • set.isdisjoint()

  • No common elements

{1,2}.isdisjoint({3,4})     # True

4.3.7.2. Subset

  • set.issubset()

  • All elements in both

{1,2} <= {3,4}              # False
{1,2} < {3,4}               # False
{1,2} <= {1,2}              # True
{1,2} <= {1,2,3}            # True
{1,2,3} <= {1,2}            # False
{1,2} < {1,2}               # False
{1,2} < {1,2,3}             # True
{1,2,3} < {1,2}             # False

4.3.7.3. Superset

  • set.issuperset()

  • All elements of b are in a

{1,2} > {1,2}               # False
{1,2} > {1,2,3}             # False
{1,2,3} > {1,2}             # True
{1,2} >= {1,2}              # True
{1,2} >= {1,2,3}            # False
{1,2,3} >= {1,2}            # True

4.3.8. Basic Operations

4.3.8.1. Union

  • set.union()

  • add

{1,2} | {1,2}               # {1, 2}
{1,2,3} | {1,2}             # {1, 2, 3}
{1,2,3} | {1,2,4}           # {1, 2, 3, 4}
{1,2} | {1,3} | {2,4}       # {1, 2, 3, 4}

4.3.8.2. Difference

  • set.difference()

  • subtract

{1,2} - {2,3}               # {1}
{1,2} - {2,3} - {3}         # {1}
{1,2} - {1,2,3}             # set()

4.3.8.3. Symmetric Difference

  • set.symmetric_difference()

  • not common elements from each

{1,2} ^ {1,2}               # set()
{1,2} ^ {2,3}               # {1, 3}
{1,2} ^ {1,3}               # {2, 3}

4.3.8.4. Intersection

  • set.intersection()

  • common element from each

{1,2} & {2,3}               # {2}
{1,2} & {2,3} & {2,4}       # {2}
{1,2} & {2,3} & {3}         # set()

4.3.9. Cardinality

my_set = {1, 2, 3}

len(my_set)
# 3

4.3.10. Assignments

4.3.10.1. Create

  • Complexity level: easy

  • Lines of code to write: 13 lines

  • Estimated time of completion: 5 min

  • Solution: solution/set_create.py

English
  1. For given data input (see below)

  2. Create set representing first row

  3. Values from second row add to set using .add()

  4. From third row create set and add it with .update()

  5. From fourth row create tuple and add it with .update()

  6. From fifth row create list and add it with .update()

Polish
  1. Dla danych wejściowych (patrz sekcja input)

  2. Stwórz my_set: set reprezentujący pierwszy wiersz

  3. Wartości z drugiego wiersza dodawaj do my_set za pomocą .add()

  4. Na podstawie trzeciego wiersza stwórz set i dodaj go za pomocą .update()

  5. Na podstawie czwartego wiersza stwórz tuple i dodaj go za pomocą .update()

  6. Na podstawie piątego wiersza stwórz list i dodaj go za pomocą .update()

Input
Table 9. Input data

Row

Sepal length

Sepal width

Petal length

Petal width

Species

1

5.8

2.7

5.1

1.9

virginica

2

5.1

3.5

1.4

0.2

setosa

3

5.7

2.8

4.1

1.3

versicolor

4

6.3

2.9

5.6

1.8

virginica

5

6.4

3.2

4.5

1.5

versicolor

The whys and wherefores
  • Defining set

  • Basic set methods