5.5. Mapping Items

5.5.1. Rationale

  • Key-Value Pairs

5.5.2. Get Key-Value Pairs

In Python 2, the methods items(), keys() and values() used to "take a snapshot" of the dictionary contents and return it as a list. It meant that if the dictionary changed while you were iterating over the list, the contents in the list would not change. In Python 3, these methods return a view object whose contents change dynamically as the dictionary changes. Therefore, in order for the behavior of iterations over the result of these methods to remain consistent with previous versions, an additional call to list() has to be performed in Python 3 to "take a snapshot" of the view object contents. 1

>>> crew = {
...    'commander': 'Melissa Lewis',
...    'botanist': 'Mark Watney',
...    'pilot': 'Rick Martinez',
... }
>>>
>>>
>>> crew.items()
dict_items([('commander', 'Melissa Lewis'), ('botanist', 'Mark Watney'), ('pilot', 'Rick Martinez')])
>>>
>>> list(crew.items())  
[('commander', 'Melissa Lewis'),
 ('botanist', 'Mark Watney'),
 ('pilot', 'Rick Martinez')]

5.5.3. References

1

Frédéric Hamidi. Why does Python 3 need dict.items to be wrapped with list()? Retrieved: 2021-02-28. URL: https://stackoverflow.com/a/17695716

5.5.4. Assignments

Code 5.3. Solution
"""
* Assignment: Mapping Items List
* Required: yes
* Complexity: easy
* Lines of code: 3 lines
* Time: 3 min

English:
    1. Define `keys: list[str]` with list of `DATA` keys
    2. Define `values: list[str]` with list of `DATA` values
    3. Define `items: list[tuple]` with list of `DATA` key-value pairs
    4. Run doctests - all must succeed

Polish:
    1. Zdefiniuj `keys: list[str]` z listą kluczy z `DATA`
    2. Zdefiniuj `values: list[str]` z listą wartości z `DATA`
    3. Zdefiniuj `items: list[tuple]` z listą par klucz-wartość z `DATA`
    4. Uruchom doctesty - wszystkie muszą się powieść

Hints:
    * `list()`
    * `dict.keys()`
    * `dict.values()`
    * `dict.items()`

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> assert type(keys) is list, \
    'Variable `keys` has invalid type, should be list'

    >>> assert type(values) is list, \
    'Variable `values` has invalid type, should be list'

    >>> assert type(items) is list, \
    'Variable `items` has invalid type, should be list'

    >>> assert all(type(x) is tuple for x in items), \
    'All rows in items should be tuples'

    >>> keys
    ['Sepal length', 'Sepal width', 'Petal length', 'Petal width']

    >>> values
    [5.8, 2.7, 5.1, 1.9]

    >>> items  # doctest: +NORMALIZE_WHITESPACE
    [('Sepal length', 5.8),
     ('Sepal width', 2.7),
     ('Petal length', 5.1),
     ('Petal width', 1.9)]
"""

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

# list[str]: with keys from DATA
keys = ...

# list[float]: with values from DATA
values = ...

# list[tuple]: with key-value pairs from DATA
items = ...