3. set

  • Only unique values

  • Mutable - can add, remove, and modify items

3.1. Initializing

  • Do not record element position

  • Do not record order of insertion

  • Do not support indexing

  • Do not support slicing

3.1.1. Initialize empty

  • Defining only with set()

my_set = set()

3.1.2. Initialize with one element

  • Comma after last element is optional

  • Brackets are required

my_set = {1}
my_set = {1,}

3.1.3. Initialize with many elements

  • Brackets are required

  • Only unique values

  • Can store elements of any hashable types

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}

3.2. Adding items

3.2.1. Adding one element

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}

3.2.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}

3.3. Popping items

  • Gets and remove items

my_set = {1, 2, 3}

value = my_set.pop()

my_set  # {1, 2}
value   # 3

3.4. Converting to set deduplicate items

3.4.1. Converting list to set

Listing 1. Converting list to set deduplicate items
names = [
    'Twardowski',
    'Иванович',
    'Jiménez',
    'Twardowski'
]

unique_names = set(names)
# {'Twardowski', 'Иванович', 'Jiménez'}

3.4.2. Converting tuple to set

Listing 2. Converting tuple to set deduplicate items
names = (
    'Twardowski',
    'Иванович',
    'Jiménez',
    'Twardowski'
)

unique_names = set(names)
# {'Twardowski', 'Иванович', 'Jiménez'}

3.5. Length of a set

my_set = {1, 2, 3}

len(my_set)
# 3

3.6. Assignments

3.6.1. Create

  • Complexity level: easy

  • Lines of code to write: 13 lines

  • Estimated time of completion: 5 min

  • Filename: 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 poniżej)

  2. Stwórz set reprezentujący pierwszy wiersz

  3. Wartości z drugiego wiersza dodawaj do 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 16. 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