7.2. Mapping Items
.keys()
- get dictionary keys.values()
- get dictionary values.items()
- get dictionary items (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. [2]
7.2.1. Dict Keys
only hashable objects can be a key
list
,set
anddict
cannot be a key
Why must dictionary keys be immutable? [1]
The hash table implementation of dictionaries uses a hash value calculated from the key value to find the key. If the key were a mutable object, its value could change, and thus its hash could also change. But since whoever changes the key object can't tell that it was being used as a dictionary key, it can't move the entry around in the dictionary. Then, when you try to look up the same object in the dictionary it won't be found because its hash value is different. If you tried to look up the old value it wouldn't be found either, because the value of the object found in that hash bin would be different.
7.2.2. Key Types
numeric types -
int
,float
logic types -
bool
,None
string types -
str
iterable types -
tuple
(not working) unhashable types -
list
,set
,dict
Numeric keys:
>>> data = {
... 1: 'red',
... 2: 'green',
... 3: 'blue',
... }
>>> data = {
... 1.1: 'red',
... 2.2: 'green',
... 3.3: 'blue',
... }
String type keys:
>>> data = {
... 'a': 'red',
... 'b': 'green',
... 'c': 'blue',
... }
Logic type keys:
>>> data = {
... True: 'red',
... False: 'green',
... None: 'blue',
... }
Iterable type keys:
>>> data = {
... (1,2,3): 'red',
... (4,5,6): 'green',
... (7,8,9): 'blue',
... }
Unhashable types (not working):
>>> data = {
... [1,2,3]: 'red',
... [4,5,6]: 'green',
... [7,8,9]: 'blue',
... }
Traceback (most recent call last):
TypeError: unhashable type: 'list'
>>> data = {
... {1,2,3}: 'red',
... {4,5,6}: 'green',
... {7,8,9}: 'blue',
... }
Traceback (most recent call last):
TypeError: unhashable type: 'set'
>>> data = {
... {1: None, 2:None, 3:None}: 'red',
... {4: None, 5:None, 6:None}: 'green',
... {7: None, 8:None, 9:None}: 'blue',
... }
Traceback (most recent call last):
TypeError: unhashable type: 'dict'
7.2.3. Get Keys
.keys()
- get dictionary keys
>>> data = {'firstname': 'Mark', 'lastname': 'Watney', 'age': 41}
>>>
>>> data.keys()
dict_keys(['firstname', 'lastname', 'age'])
>>>
>>> list(data.keys())
['firstname', 'lastname', 'age']
7.2.4. Get Values
.values()
- get dictionary values
>>> data = {'firstname': 'Mark', 'lastname': 'Watney', 'age': 41}
>>>
>>> data.values()
dict_values(['Mark', 'Watney', 41])
>>>
>>> list(data.values())
['Mark', 'Watney', 41]
7.2.5. Get Items
.items()
- get dictionary items (key-value pairs)
>>> data = {'firstname': 'Mark', 'lastname': 'Watney', 'age': 41}
>>>
>>> data.items()
dict_items([('firstname', 'Mark'), ('lastname', 'Watney'), ('age', 41)])
>>>
>>> list(data.items())
[('firstname', 'Mark'), ('lastname', 'Watney'), ('age', 41)]
7.2.6. Use Case - 1
>>> calendarium = {
... 1961: 'First Human Space Flight',
... 1969: 'First Step on the Moon',
... }
7.2.7. Use Case - 2
>>> factorial = {
... 0: 1,
... 1: 1,
... 2: 2,
... 3: 6,
... 4: 24,
... 5: 120,
... 6: 720,
... }
7.2.8. Use Case - 3
>>> addition = {
... (1, 2): 3,
... (3, 2): 5,
... (3, 5): 8,
... }
7.2.9. Use Case - 4
>>> users = {
... ('Melissa', 'Lewis'): 'mlewis',
... ('Mark', 'Watney'): 'mwatney',
... ('Alex', 'Vogel'): 'avogel',
... ('Rick', 'Martinez'): 'rmartinez',
... ('Beth', 'Johanssen'): 'bjohanssen',
... ('Chris', 'Beck'): 'cbeck',
... }
7.2.10. Use Case - 5
>>> data = {
... (5.8, 2.7, 5.1, 1.9): 'virginica',
... (5.1, 3.5, 1.4, 0.2): 'setosa',
... (5.7, 2.8, 4.1, 1.3): 'versicolor',
... (6.3, 2.9, 5.6, 1.8): 'virginica',
... (6.4, 3.2, 4.5, 1.5): 'versicolor',
... (4.7, 3.2, 1.3, 0.2): 'setosa',
... (7.0, 3.2, 4.7, 1.4): 'versicolor',
... (7.6, 3.0, 6.6, 2.1): 'virginica',
... (4.6, 3.1, 1.5, 0.2): 'setosa',
... }
7.2.11. Use Case - 6
>>> from pprint import pprint
>>>
>>>
>>> data = {
... 'firstname': 'Mark',
... 'lastname': 'Watney',
... 'group': 'users',
... }
>>>
>>> pprint(list(data.items()), width=30)
[('firstname', 'Mark'),
('lastname', 'Watney'),
('group', 'users')]
7.2.12. References
7.2.13. Assignments
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Mapping Dict KeysValuesItems
# - Difficulty: easy
# - Lines: 3
# - Minutes: 2
# %% English
# 1. Use `DATA: dict`
# 2. Define variables:
# - `result_a: list[str]` with keys from `DATA`
# - `result_b: list[str]` with values from `DATA`
# - `result_c: list[tuple]` with items from `DATA`
# 3. Run doctests - all must succeed
# %% Polish
# 1. Użyj `DATA: dict`
# 2. Zdefiniuj zmienne:
# - `result_a: list[str]` z kluczami z `DATA`
# - `result_b: list[str]` z wartościami z `DATA`
# - `result_c: list[tuple]` z elementami z `DATA`
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - `list()`
# - `dict.keys()`
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> from pprint import pprint
>>> assert type(result_a) is list, \
'Variable `result_a` has invalid type, should be list'
>>> assert all(type(x) is str for x in result_a), \
'All elements in `result_a` should be str'
>>> assert type(result_b) is list, \
'Variable `result_b` has invalid type, should be list'
>>> assert all(type(x) is str for x in result_b), \
'All elements in `result_b` should be str'
>>> assert type(result_c) is list, \
'Variable `result_c` has invalid type, should be list'
>>> assert all(type(x) is tuple for x in result_c), \
'All elements in `result_c` should be tuple'
>>> pprint(result_a)
['firstname', 'lastname', 'group']
>>> pprint(result_b)
['Mark', 'Watney', 'users']
>>> pprint(result_c, width=40)
[('firstname', 'Mark'),
('lastname', 'Watney'),
('group', 'users')]
"""
DATA = {
'firstname': 'Mark',
'lastname': 'Watney',
'group': 'users',
}
# Keys from `DATA`
# type: list[str]
result_a = ...
# Values from `DATA`
# type: list[str]
result_b = ...
# Items from `DATA`
# type: list[tuple[str,str]]
result_c = ...