3.3. Sequence Set

3.3.1. Rationale

  • Only unique values

  • Mutable - can add, remove, and modify items

  • Can store elements of any hashable types

3.3.2. Type Definition

  • Set is unordered data structure (each time order differs)

  • Do not record element position

  • Do not record order of insertion

  • Do not support getitem

  • Do not support slice

  • Defining only with set() - no short syntax

  • Comma after last element is optional

  • Brackets are required

Listing 3.5. set type definition
data = set()

data = {1}
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}
Listing 3.6. set stores only unique values.
{1, 2, 1}                       # {1, 2}
{1, 2, 'a'}                     # {1, 2, 'a'}
{1, 2, (3, 4)}                  # {1, 2, (3, 4)}
{1, 2, [3, 4]}                  # TypeError: unhashable type: 'list'
{1, 2, {3, 4}}                  # TypeError: unhashable type: 'set'
{1, 2, frozenset({3, 4})}       # {1, 2, frozenset({3, 4})}
Listing 3.7. set compares by values, not types
{1}                             # {1}
{1.0}                           # {1.0}
{1, 1.0}                        # {1}
{1.0, 1}                        # {1.0}

3.3.3. Type Casting

  • set() converts argument to set

data = [1, 2, 3]
set(data)
# {1, 2, 3}
data = (1, 2, 3)
set(data)
# {1, 2, 3}
data = {1, 2, 3}
set(data)
# {1, 2, 3}
data = frozenset({1, 2, 3})
set(data)
# {1, 2, 3}

3.3.4. Add

data = {1, 2}

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

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

data.add(4)
# {1, 2, 3, 4}
data = {1, 2}
data.add([3, 4])
# TypeError: unhashable type: 'list'
data = {1, 2}
data.add((3, 4))
# {1, 2, (3, 4)}
data = {1, 2}
data.add({3, 4})
# TypeError: unhashable type: 'set'
data = {1, 2}
data.add(frozenset({3,4}))
# {frozenset({3, 4}), 1, 2}

3.3.5. Update

data = {1, 2}
# {1, 2}

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

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

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

3.3.6. Pop

  • Gets and remove items

data = {1, 2, 3}

value = data.pop()

print(data)
# {1, 2}

print(value)
# 3

3.3.7. Deduplicate

  • Works with tuple, list, str

data = [1,2,3,1,1,2,4]

set(data)
# {1, 2, 3, 4}
Listing 3.8. Converting set deduplicate items
data = [
    'Twardowski',
    'Twardowski',
    'Watney',
    'Twardowski'
]

set(data)
# {'Twardowski', 'Watney'}

3.3.8. Membership

3.3.8.1. Is Disjoint?

  • True - if thre are no common elements in data and x

  • False - if any x element are in data

data = {1,2}

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

3.3.8.2. Is Subset?

  • True - if x has all elements from data

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

data = {1,2}

data.issubset({1})          # False
data.issubset({1,2})        # True
data.issubset({1,2,3})      # True
data.issubset({1,3,4})      # False
{1,2} < {3,4}               # False
{1,2} < {1,2}               # False
{1,2} < {1,2,3}             # True
{1,2,3} < {1,2}             # False
{1,2} <= {3,4}              # False
{1,2} <= {1,2}              # True
{1,2} <= {1,2,3}            # True
{1,2,3} <= {1,2}            # False

3.3.8.3. Is Superset?

  • True - if data has all elements from x

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

data = {1,2}

data.issuperset({1})        # True
data.issuperset({1,2})      # True
data.issuperset({1,2,3})    # False
data.issuperset({1,3})      # False
data.issuperset({2,1})      # True
{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

3.3.9. Basic Operations

3.3.9.1. Union

  • returns sum of elements from data and x

data = {1,2}

data.union({1,2})           # {1, 2}
data.union({1,2,3})         # {1, 2, 3}
data.union({1,2,4})         # {1, 2, 4}
data.union({1,3}, {2,4})    # {1, 2, 3, 4}
{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}

3.3.9.2. Difference

  • returns elements from data which are not in x

data = {1,2}

data.difference({1,2})          # set()
data.difference({1,2,3})        # set()
data.difference({1,4})          # {2}
data.difference({1,3}, {2,4})   # set()
data.difference({3,4})          # {1, 2}
{1,2} - {2,3}                   # {1}
{1,2} - {2,3} - {3}             # {1}
{1,2} - {1,2,3}                 # set()

3.3.9.3. Symmetric Difference

  • returns elements from data and x, but without common

data = {1,2}

data.symmetric_difference({1,2})           # set()
data.symmetric_difference({1,2,3})         # {3}
data.symmetric_difference({1,4})           # {2, 4}
data.symmetric_difference({1,3}, {2,4})    # TypeError: symmetric_difference() takes exactly one argument (2 given)
data.symmetric_difference({3,4})           # {1, 2, 3, 4}
{1,2} ^ {1,2}               # set()
{1,2} ^ {2,3}               # {1, 3}
{1,2} ^ {1,3}               # {2, 3}

3.3.9.4. Intersection

  • common element from in data and x

data = {1,2}

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

3.3.10. Cardinality

data = {1, 2, 3}

len(data)
# 3

3.3.11. Assignments

3.3.11.1. Set Create

English
  1. Create set result with elements:

    • 'a'

    • 1

    • 2.2

  2. Print result

  3. Print number of elements in result

Polish
  1. Stwórz zbiór result z elementami:

    • 'a'

    • 1

    • 2.2

  2. Wypisz result

  3. Wypisz liczbę elementów result

3.3.11.2. Set Many

English
  1. Use data from "Input" section (see below)

  2. Create set result representing first row

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

  4. From third row create set and add it to result using .update()

  5. From fourth row create tuple and add it to result using .update()

  6. From fifth row create list and add it to result using .update()

  7. Print sorted result

  8. Compare result with "Output" section (see below)

Polish
  1. Użyj danych z sekcji "Input" (patrz poniżej)

  2. Stwórz zbiór result reprezentujący pierwszy wiersz

  3. Wartości z drugiego wiersza dodawaj do result używając .add()

  4. Na podstawie trzeciego wiersza stwórz set i dodaj go do result używając .update()

  5. Na podstawie czwartego wiersza stwórz tuple i dodaj go do result używając .update()

  6. Na podstawie piątego wiersza stwórz list i dodaj go do result używając .update()

  7. Wypis posortowany result

  8. Porównaj wyniki z sekcją "Output" (patrz poniżej)

Input
Table 3.2. Input data

Row

Sepal length

Sepal width

Petal length

Petal width

1

5.8

2.7

5.1

1.9

2

5.1

3.5

1.4

0.2

3

5.7

2.8

4.1

1.3

4

6.3

2.9

5.6

1.8

5

6.4

3.2

4.5

1.5

Output
result: set
# {0.2, 1.3, 1.4, 1.5, 1.8, 1.9, 2.7, 2.8, 2.9, 3.2, 3.5, 4.1, 4.5, 5.1, 5.6, 5.7, 5.8, 6.3, 6.4}
The whys and wherefores
  • Defining set

  • Basic set methods