5.1. Mapping dict

  • dict are key-value storage

  • Mutable - can add, remove, and modify items

5.1.1. Type Definition

  • {} is used more often

  • dict() is more readable

  • Comma after last element is optional

  • Since Python 3.7 dict keeps order of elements

  • Before Python 3.7 dict order is not ensured!!

my_dict = {}
my_dict = dict()
Listing 67. Initialize with many elements
my_dict = {
    'first_name': 'Jan',
    'last_name': 'Twardowski'
}

my_dict = dict(
    first_name='Jan',
    last_name='Twardowski'
)
Listing 68. Duplicating items are overridden by latter
my_dict = {
    'species': 'setosa',
    'species': 'virginica',
}
# {'species': 'virginica'}

5.1.2. Type Annotation

my_dict: dict = {}
my_dict: dict = dict()
from typing import Dict

my_dict: Dict[int, str] = {
    0: 'setosa',
    1: 'virginica',
    2: 'versicolor',
}
from typing import Dict

my_dict: Dict[float, str] = {
    5.8: 'Sepal length',
    2.7: 'Sepal width',
    5.1: 'Petal length',
    1.9: 'Petal width',
}
from typing import Dict

my_dict: Dict[str, float] = {
    'Sepal length': 5.8,
    'Sepal width': 2.7,
    'Petal length': 5.1,
    'Petal width': 1.9,
}

5.1.3. Contains

my_dict = {
    'first_name': 'Jan',
    'last_name': 'Twardowski',
}

'first_name' in my_dict
# True

'agency' in my_dict
# False

5.1.4. Getting Items

  • [...] throws KeyError exception if key not found in dict

  • .get() returns None if not found, but also allows to set default value

5.1.4.1. Getitem Method

my_dict = {
    'first_name': 'Jan',
    'last_name': 'Twardowski',
}

my_dict['last_name']
# Twardowski

my_dict['agency']
# KeyError: 'agency'
my_dict = {
    1961: 'First Human Space Flight',
    1969: 'First Step on the Moon',
}

my_dict[1961]
# 'First Human Space Flight'

my_dict['1961']
# KeyError: '1961'

5.1.4.2. Get Method

  • .get() returns None if key not found

  • .get() can have default value, if key not found

my_dict = {
    'first_name': 'Jan',
    'last_name': 'Twardowski',
}

my_dict.get('last_name')
# Twardowski

my_dict.get('agency')
# None

my_dict.get('agency', 'n/a')
# 'n/a'
my_dict = {
    1961: 'First Human Space Flight',
    1969: 'First Step on the Moon',
}

my_dict.get(1961)
# 'First Human Space Flight'

my_dict.get('1961')
# None

my_dict.get('1961', 'unknown')
# 'unknown'

5.1.4.3. Get Keys, Values and Key-Value Pairs

  • Key can be any hashable object

my_dict = {
    'Sepal length': 5.8,
    'Sepal width': 2.7,
    'Petal length': 5.1,
    'Petal width': 1.9,
}

my_dict.keys()
# dict_keys(['Sepal length', 'Sepal width', 'Petal length', 'Petal width'])

list(my_dict.keys())
# ['Sepal length', 'Sepal width', 'Petal length', 'Petal width', 'Species']

my_dict.values()
# dict_values([5.8, 2.7, 5.1, 1.9])

list(my_dict.values())
# [5.8, 2.7, 5.1, 1.9]

my_dict.items()
# dict_items([
#     ('Sepal length', 5.8),
#     ('Sepal width', 2.7),
#     ('Petal length', 5.1),
#     ('Petal width', 1.9),
# ])

list(my_dict.items())
# [
#     ('Sepal length', 5.8),
#     ('Sepal width', 2.7),
#     ('Petal length', 5.1),
#     ('Petal width', 1.9),
#     ('Species', 'virginica'),
# ]

5.1.5. Setting Items

  • Adds if value not exist

  • Updates if value exist

Listing 69. Setitem Method
my_dict = {
    'first_name': 'Jan',
    'last_name': 'Twardowski',
}

my_dict['agency'] = 'POLSA'

print(my_dict)
# {
#   'first_name': 'Jan',
#   'last_name': 'Twardowski',
#   'agency': 'POLSA'
# }
Listing 70. Update Method
my_dict = {
    'first_name': 'Jan',
    'last_name': 'Twardowski',
}

my_dict.update(agency='POLSA')
print(my_dict)
# {
#   'first_name': 'Jan',
#   'last_name': 'Twardowski',
#   'agency': 'POLSA'
# }

my_dict.update(mission=['Apollo', 'Artemis', 'Ares'])
print(my_dict)
# {
#   'first_name': 'Jan',
#   'last_name': 'Twardowski',
#   'agency': 'POLSA',
#   'mission': ['Apollo', 'Artemis', 'Ares']
# }
Listing 71. Update Method
my_dict = {
    'first_name': 'Jan',
    'last_name': 'Twardowski',
}

data = {
    'agency': 'POLSA',
    'mission': ['Apollo', 'Artemis', 'Ares'],
}

my_dict.update(data)
print(my_dict)
# {
#   'first_name': 'Jan',
#   'last_name': 'Twardowski',
#   'agency': 'POLSA',
#   'mission': ['Apollo', 'Artemis', 'Ares']
# }

5.1.6. Deleting Items

Listing 72. Pop Method
my_dict = {
    'first_name': 'Jan',
    'last_name': 'Twardowski',
    'agency': 'POLSA',
}

value = my_dict.pop('agency')

print(my_dict)
# {'first_name', 'Jan',
#  'last_name': 'Twardowski'}

print(value)
# 'POLSA'
Listing 73. Del Keyword
my_dict = {
    'first_name': 'Jan',
    'last_name': 'Twardowski',
    'agency': 'POLSA',
}

del my_dict['agency']

print(my_dict)
# {'first_name': 'Jan',
#  'last_name': 'Twardowski'}

5.1.7. Indexing and Slicing

  • Indexing on dict is not possible

  • Slicing on dict is not possible

DATA = {
    'a': 0,
    'b': 1,
    'c': 2,
}

DATA[0]             # KeyError: 0
DATA[1]             # KeyError: 1
DATA[2]             # KeyError: 2

DATA[-0]            # KeyError: 0
DATA[-1]            # KeyError: -1
DATA[-2]            # KeyError: -2

DATA[1:2]           # TypeError: unhashable type: 'slice'
DATA[:2]            # TypeError: unhashable type: 'slice'
DATA[::2]           # TypeError: unhashable type: 'slice'
DATA = {
    0: 'a',
    1: 'b',
    2: 'c',
}

DATA[0]             # 'a'
DATA[1]             # 'b'
DATA[2]             # 'c'

DATA[-0]            # 'a'
DATA[-1]            # KeyError: -1
DATA[-2]            # KeyError: -2

DATA[1:2]           # TypeError: unhashable type: 'slice'
DATA[:2]            # TypeError: unhashable type: 'slice'
DATA[::2]           # TypeError: unhashable type: 'slice'

5.1.8. dict vs. set

  • Both set and dict keys must be hashable

  • Both set and dict uses the same { and } braces

  • Despite similar syntax, they are different types

{1, 2}            # set
{1: 2}            # dict
{1, 2, 3, 4}      # set
{1: 2, 3: 4}      # dict

{1, 2,}           # set
{1: 2,}           # dict
Listing 74. Empty dict
my_data = {1: 1}
# {1:1}

my_data.pop(1)
# {}
Listing 75. Empty set
my_data = {1}
# {1}

my_data.pop()
# set()
Listing 76. Differences
my_data = {1: 1}
isinstance(my_data, dict)         # True
isinstance(my_data, set)          # False

my_data = {1}
isinstance(my_data, dict)         # False
isinstance(my_data, set)          # True

my_data = {}
isinstance(my_data, (set, dict))  # True
isinstance(my_data, dict)         # True
isinstance(my_data, set)          # False

5.1.9. Length

my_dict = {
    'first_name': 'Jan',
    'last_name': 'Twardowski',
    'age': 42,
}

len(my_dict)
# 3

len(my_dict.keys())
# 3

len(my_dict.values())
# 3

len(my_dict.items())
# 3

5.1.10. Examples

git = {
    'ce16a8ce': 'commit/1',
    'cae6b510': 'commit/2',
    '895444a6': 'commit/3',
    'aef731b5': 'commit/4',
    '4a92bc79': 'branch/master',
    'b3bbd85a': 'tag/v1.0',
}

5.1.11. Assignments

5.1.11.1. Aviation Language

  • Complexity level: easy

  • Lines of code to write: 3 lines

  • Estimated time of completion: 5 min

  • Solution: solution/dict_alphabet.py

English
  1. Create translator of pilot's alphabet

  2. Each letter has it's phonetic counterpart

  3. To convert table use multiline select with alt key in your IDE (if shortcut key is not working in your IDE, use only first four letters)

  4. Ask user to input letter

  5. User will always put only one capitalized letter or number

  6. Print phonetic letter pronunciation

  7. If user type character not existing in alphabet, print: "Pilots don't say that"

  8. Do not use if, try, and except

Polish
  1. Stwórz tłumacza alfabetu pilotów

  2. Pojedynczym literom przyporządkuj ich fonetyczne odpowiedniki

  3. Do przekonwertowania tabelki wykorzystaj zaznaczanie wielu linijek za pomocą klawisza alt w Twoim IDE (jeżeli skrót klawiszowy nie działa w Twoim IDE, użyj tylko cztery pierwsze litery)

  4. Poproś użytkownika o wprowadzenie litery

  5. Użytkownik zawsze poda tylko jedną dużą literę lub cyfrę

  6. Wypisz fonetyczną wymowę litery

  7. Jeżeli wpisał znak, który nie występuje w alfabecie, wypisz: "Pilots don't say that"

  8. Nie używaj if, try ani except

Input
Letter, Pronounce
A, Alfa
B, Bravo
C, Charlie
D, Delta
E, Echo
F, Foxtrot
G, Golf
H, Hotel
I, India
J, Juliet
K, Kilo
L, Lima
M, Mike
N, November
O, Oscar
P, Papa
Q, Quebec
R, Romeo
S, Sierra
T, Tango
U, Uniform
V, Victor
W, Whisky
X, X-Ray
Y, Yankee
Z, Zulu
The whys and wherefores
  • Defining dict with values

  • Type casting