liveBook · Manning

Map

As explained in Python How-To

A map is an associative data structure that stores key-value pairs and supports retrieving values by key.

Need to find a task’s urgency from its title? Use a Python dictionary, or `dict`, to store title–urgency pairs. Unlike a list or tuple, which holds individual items, a dictionary stores keys with corresponding values. The key gives you a way to retrieve the value. section 3.4

Look up task urgency

```python urgencies = {"Laundry": 3, "Homework": 5, "Museum": 2} print(urgencies["Homework"]) ``` Output: `5`. Use subscripting when you created the dictionary and want a misspelled key to surface as an error. For a dictionary supplied by someone else, `get` retrieves an item without raising `KeyError` when the key is absent. section 3.4 section 3.7

Choose a construction pattern that matches the data you already have:

  • Use `dict(zip(id_numbers, titles))` for parallel iterables. `zip` supplies two-item pairs; `dict` takes the first item of each pair as a key and the second as its value. section 5.1.3
  • Use `{task.title: task.description for task in tasks}` when each existing item supplies both parts. A dictionary comprehension makes that transformation concise. section 5.2.2
  • Use dictionary views when accessing all keys, values, or pairs; the views stay in sync with their dictionary. section 3.7

Keys must be hashable: a list used as a key raises `TypeError`. Dictionaries and sets use hash tables, making item lookup efficient at O(1) time complexity. A dictionary cannot retain duplicate keys; when constructing one with repeated keys, the later value overrides the earlier value. section 3.5 section 3.5.3 section 3.5.4

Replace branches with a function lookup

```python actions = {"mean": get_mean, "min": get_min, "max": get_max} calculation = actions.get(action, fallback_action) processed = calculation(data) ``` Here the values are functions. Looking up the requested action avoids a growing `if`/`elif` chain, while `get` supplies a fallback for an unknown action. section 7.2.1

A dictionary can also label fields: `csv.DictReader` reads a CSV header as keys and produces a dictionary for each subsequent row. For a lightweight record, those keys make fields clearer than list positions, though mistyping a key can cause `KeyError`. section 11.2.2 section 3.3.1

How Python How-To covers it

How deep it goes
Explains construction, lookup choices, hashable keys, and O(1) retrieval without detailing hash-table implementation.
Examples
Uses task urgencies, zipped task IDs and titles, function dispatch, and header-based CSV rows.
What it assumes
Assumes familiarity with lists, tuples, and loops while explaining how paired iterables and comprehensions create dictionaries.
What you can do after
Build it from scratch with literals, zip, or comprehensions, then apply it to keyed lookup and labeled records.
How it connects
Connects dictionaries to iterables, dictionary comprehensions, hash tables, sets, and keyword arguments.

Also explained in

Compare how 42 books explain Map →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python How-To. The section links point to the parts of the book it draws on.