5.2. Mapping Generate

5.2.1. List of pairs

pairs = [
    (1, 2)
]

dict(pairs)
# {1: 2}
pairs = [
    ('a', 1),
    ('b', 2)
]

dict(pairs)
# {'a': 1, 'b': 2}
pairs = [
    ('first_name', 'Jan'),
    ('last_name', 'Twardowski'),
]

dict(pairs)
# {
#   'first_name': 'Jan',
#   'last_name': 'Twardowski'
# }
pairs = [
    ('Sepal length', 5.8),
    ('Sepal width', 2.7),
    ('Petal length', 5.1),
    ('Petal width', 1.9),
    ('Species', 'virginica')
]

dict(pairs)
# {
#     'Sepal length': 5.8,
#     'Sepal width': 2.7,
#     'Petal length': 5.1,
#     'Petal width': 1.9,
#     'Species': 'virginica'
# }

5.2.2. Zip

  • zip is a generator

  • zip will create a list of pairs (like dict.items())

keys =  ['Sepal length', 'Sepal width', 'Petal length', 'Petal width', 'Species']
values = [5.8, 2.7, 5.1, 1.9, 'virginica']

dict(zip(keys, values))
# {
#   'Sepal length': 5.8,
#   'Sepal width': 2.7,
#   'Petal length': 5.1,
#   'Petal width': 1.9,
#   'Species': 'virginica'
# }

5.2.3. Enumerate

labels = ['setosa', 'versicolor', 'virginica']

dict(enumerate(labels))
# {
#   0: 'setosa',
#   1: 'versicolor',
#   2: 'virginica'
# }

5.2.4. Assignments

Todo

Create assignments