7.1. Mapping Dict

  • dict are key-value storage (HashMap)

  • Mutable - can add, remove, and modify items

  • Since Python 3.7: dict keeps order of elements

  • Before Python 3.7: dict order is not ensured!!

Example:

>>> user = {'username': 'alice', 'password': 'secret'}
>>> data = {'parameter': 'co2', 'value': 500, 'unit': 'ppm'}
>>> data = {'name': 'Alice', 'bp': '120/80', 'hr': 70, 'height': 170, 'weight': 55.5}
>>> product = {'name': 'MyProduct', 'usd': 100, 'eur': 90, 'pln': 400}

How are dictionaries implemented in CPython? [1]

CPython's dictionaries are implemented as resizable hash tables. Compared to B-trees, this gives better performance for lookup (the most common operation by far) under most circumstances, and the implementation is simpler.

Dictionaries work by computing a hash code for each key stored in the dictionary using the hash() built-in function. The hash code varies widely depending on the key and a per-process seed; for example, "Python" could hash to -539294296 while "python", a string that differs by a single bit, could hash to 1142331976. The hash code is then used to calculate a location in an internal array where the value will be stored. Assuming that you're storing keys that all have different hash values, this means that dictionaries take constant time – O(1), in Big-O notation – to retrieve a key.

../../_images/type-dict-hashmap.png

7.1.1. Define Empty

  • {} is faster

  • dict() is more verbose

>>> data = {}
>>> data = dict()

7.1.2. Define With Elements

  • Two ways of creating dict with elements

  • Comma after last element is optional

>>> data = {
...     'username': 'alice',
...     'password': 'secret',
... }
>>> data = dict(
...     username='alice',
...     password='secret',
... )

7.1.3. Define from List of Pairs

Pair:

>>> pair1 = ('username', 'alice')
>>> pair2 = ('password', 'secret')

List of pairs:

>>> pairs = [
...     ('username', 'alice'),
...     ('password', 'secret'),
... ]

Convert list of pairs to dict:

>>> dict(pairs)
{'username': 'alice', 'password': 'secret'}

7.1.4. Duplicates

Duplicating items are overridden by latter:

>>> data = {
...     'username': 'alice',
...     'username': 'bob',
... }
>>>
>>> data
{'username': 'bob'}

7.1.5. 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

set() values behaves similar to dict() keys. This is one of the reasons why they both have the same brackets. However both structures are ment to store different types of data: sets store values, such as usernames, and dicts store key-value pairs, such as user details.

>>> data = {'alice', 'bob', 'carol'}  # set
>>> data = {'username': 'alice', 'password': 'secret'}  # dict

7.1.6. Length

>>> data = {
...     'username': 'alice',
...     'password': 'secret',
... }
>>>
>>>
>>> len(data)
2

7.1.7. Use Case - 1

>>> MONTHS = {
...     1: 'January',
...     2: 'February',
...     3: 'March',
...     4: 'April',
...     5: 'May',
...     6: 'June',
...     7: 'July',
...     8: 'August',
...     9: 'September',
...     10: 'October',
...     11: 'November',
...     12: 'December'
... }

7.1.8. Use Case - 2

  • GIT - version control system

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

7.1.9. Recap

  • dict are key-value storage (HashMap)

  • Mutable - you can add, modify and remove items

  • Empty dict: data = {} or data = dict()

7.1.10. References

7.1.11. Assignments

# %% About
# - Name: Mapping Dict Create
# - 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. Define `result: dict` with:
#    - key `firstname` with value `Mark`
#    - key `lastname` with value `Watney`
#    - key `group` with value `users`
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj `result: dict` z:
#    - kluczem `firstname` o wartości `Mark`
#    - kluczem `lastname` o wartości `Watney`
#    - kluczem `group` o wartości `users`
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> assert type(result) is dict, \
'Variable `result` has invalid type, should be dict'

>>> assert 'firstname' in result.keys(), \
'Value `firstname` is not in the result keys'
>>> assert 'lastname' in result.keys(), \
'Value `lastname` is not in the result keys'
>>> assert 'group' in result.keys(), \
'Value `group` is not in the result keys'

>>> assert 'Mark' in result['firstname'], \
'Value `Mark` is not in the result values'
>>> assert 'Watney' in result['lastname'], \
'Value `Watney` is not in the result values'
>>> assert 'users' in result['group'], \
'Value `users` is not in the result values'
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% Imports

# %% Types
result: dict[str,str]

# %% Data

# %% Result
result = ...

# %% About
# - Name: Mapping Dict ListOfPairs
# - Difficulty: easy
# - Lines: 1
# - 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: list[tuple]`
# 2. Define `result: dict` with `DATA` converted to `dict`
# 3. Run doctests - all must succeed

# %% Polish
# 1. Użyj `DATA: list[tuple]`
# 2. Zdefiniuj `result: dict` z przekonwertowanym `DATA` do `dict`
# 3. Uruchom doctesty - wszystkie muszą się powieść

# %% Hints
# - `dict()`

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> from pprint import pprint

>>> assert type(result) is dict, \
'Variable `result` has invalid type, should be dict'

>>> assert all(type(x) is str for x in result.keys()), \
'All dict keys should be str'

>>> assert 'firstname' in result.keys()
>>> assert 'lastname' in result.keys()
>>> assert 'group' in result.keys()

>>> assert 'Mark' in result.values()
>>> assert 'Watney' in result.values()
>>> assert 'users' in result.values()

>>> pprint(result, width=40, sort_dicts=False)
{'firstname': 'Mark',
 'lastname': 'Watney',
 'group': 'users'}
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% Imports

# %% Types
result: dict[str,str]

# %% Data
DATA = [
    ('firstname', 'Mark'),
    ('lastname', 'Watney'),
    ('group', 'users'),
]

# %% Result
result = ...