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,setanddictcannot 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,floatlogic types -
bool,Nonestring types -
striterable types -
tuple(invalid keys) 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: cannot use 'list' as a dict key (unhashable type: 'list')
>>> data = {
... {1,2,3}: 'red',
... {4,5,6}: 'green',
... {7,8,9}: 'blue',
... }
Traceback (most recent call last):
TypeError: cannot use 'set' as a dict key (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: cannot use 'dict' as a dict key (unhashable type: 'dict')
7.2.3. Get Keys
.keys()- get dictionary keys
>>> data = {
... 'firstname': 'Alice',
... 'lastname': 'Apricot',
... 'age': 30,
... }
>>>
>>> data.keys()
dict_keys(['firstname', 'lastname', 'age'])
>>>
>>> list(data.keys())
['firstname', 'lastname', 'age']
7.2.4. Get Values
.values()- get dictionary values
>>> data = {
... 'firstname': 'Alice',
... 'lastname': 'Apricot',
... 'age': 30,
... }
>>>
>>> data.values()
dict_values(['Alice', 'Apricot', 30])
>>>
>>> list(data.values())
['Alice', 'Apricot', 30]
7.2.5. Get Items
.items()- get dictionary items (key-value pairs)
>>> data = {
... 'firstname': 'Alice',
... 'lastname': 'Apricot',
... 'age': 30,
... }
>>>
>>> data.items()
dict_items([('firstname', 'Alice'), ('lastname', 'Apricot'), ('age', 30)])
>>>
>>> list(data.items())
[('firstname', 'Alice'), ('lastname', 'Apricot'), ('age', 30)]
7.2.6. Contains
in- check if element is indictkeysO(1) - constant complexity
SetUp:
>>> data = {
... 'firstname': 'Alice',
... 'lastname': 'Apricot',
... 'age': 30,
... }
Check if object is in the dict keys:
>>> 'firstname' in data.keys()
True
Check if object is in the dict values:
>>> 'Alice' in data.values()
True
Check if object is in the dict items:
>>> ('firstname', 'Alice') in data.items()
True
By default, Python will search only in dict keys:
>>> 'firstname' in data
True
>>>
>>> 'Alice' in data
False
>>>
>>> ('firstname', 'Alice') in data
False
7.2.7. Use Case - 1
Keys are
int
>>> calendarium = {
... 1961: 'First Human Space Flight',
... 1969: 'First Step on the Moon',
... }
7.2.8. Use Case - 2
Keys are
float
>>> celsius = {
... -273.15: 'absolute zero',
... 0.0: 'water freezing point',
... 36.6: 'human body temperature',
... 100.0: 'water boiling point',
... }
7.2.9. Use Case - 3
Keys are
boolandNone
>>> status = {
... True: 'success',
... False: 'failure',
... None: 'pending',
... }
7.2.10. Use Case - 4
Keys are
str
>>> color = {
... 'r': 'red',
... 'g': 'green',
... 'b': 'blue',
... }
7.2.11. Use Case - 5
Keys are
tuple
>>> iris = {
... (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.12. Use Case - 6
>>> factorial = {
... 0: 1,
... 1: 1,
... 2: 2,
... 3: 6,
... 4: 24,
... 5: 120,
... 6: 720,
... }
7.2.13. Use Case - 7
>>> addition = {
... (1, 2): 3,
... (3, 2): 5,
... (3, 5): 8,
... }
7.2.14. Use Case - 8
>>> blood_pressure = {
... (100, 72): 'ok',
... (120, 80): 'normal',
... (130, 90): 'hypertension',
... }
7.2.15. Recap
Dict key can only be a hashable object
Valid keys:
int,float,bool,None,str,tupleInvalid keys:
list,setanddictdict.keys()- get dictionary keysdict.values()- get dictionary valuesdict.items()- get dictionary items (key-value pairs)
7.2.16. References
7.2.17. Assignments
# %% About
# - Name: Mapping Dict KeysValuesItems
# - Difficulty: easy
# - Lines: 3
# - Minutes: 2
# %% 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
# %% 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ść
# %% Expected
# >>> result_a
# ['firstname', 'lastname', 'age']
#
# >>> result_b
# ['Alice', 'Apricot', 30]
#
# >>> result_c
# [('firstname', 'Alice'),
# ('lastname', 'Apricot'),
# ('age', 30)]
# %% Hints
# - `list()`
# - `dict.keys()`
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> from pprint import pprint
>>> assert type(result_a) is list, \
'Variable `result_a` has an invalid type; expected: `list`.'
>>> assert all(type(x) is str for x in result_a), \
'Variable `result_a` has elements of an invalid type; all items should be: `str`.'
>>> assert type(result_b) is list, \
'Variable `result_b` has an invalid type; expected: `list`.'
>>> assert all(type(x) in (str,int) for x in result_b), \
'All elements in `result_b` should be str or int'
>>> assert type(result_c) is list, \
'Variable `result_c` has an invalid type; expected: `list`.'
>>> assert all(type(x) is tuple for x in result_c), \
'Variable `result_c` has elements of an invalid type; all items should be: `tuple`.'
>>> pprint(result_a)
['firstname', 'lastname', 'age']
>>> pprint(result_b)
['Alice', 'Apricot', 30]
>>> pprint(result_c, width=40)
[('firstname', 'Alice'),
('lastname', 'Apricot'),
('age', 30)]
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
result_a: list[str]
result_b: list[str]
result_c: list[tuple[str,str|int]]
# %% Data
DATA = {
'firstname': 'Alice',
'lastname': 'Apricot',
'age': 30,
}
# %% Result
result_a = ...
result_b = ...
result_c = ...