liveBook · Manning

Map

As explained in A Pythonic Adventure: From Python basics to a working web app

A Python dictionary is a mutable mapping that associates unique keys with values for lookup and assignment.

A dictionary keeps related bits of information together by pairing each key with a value. Think of one coffee order: the keys can be name, drink, flavor, and topping, while the values hold the customer’s choices. Use a key to assign a value and quickly find it again. section 6.1 section 7.0

Start with an empty dictionary using curly braces: order = {}. Then fill it one small piece at a time, such as order['name'] = input("What's your name: "). Create the dictionary before adding to it; otherwise Python reports that order is not defined. Return the completed dictionary from the function so the rest of the program can use the order. section 7.0

Build one order

An order dictionary can grow as the customer makes choices. Each readable key labels what its value means, making the printed result easy to check before saving or serving the order. section 6.0 section 7.0

In [4]: order['drink'] = 'decaf'
 
In [5]: order['flavor'] = 'vanilla'
 
In [6]: order['topping'] = 'chocolate'
 
In [7]: print(order)
{'name': 'Erik', 'drink': 'decaf', 'flavor': 'vanilla',
'topping': 'chocolate'}
 
In [8]:

Inspect a live dictionary as name, drink, flavor, and topping are added one key-value pair at a time. section 6.0

Dictionaries also help collect web-form choices. A submitted form provides values through request.form using names such as "drink" as keys. Those values can be gathered into one new_order dictionary, added to an orders list, saved, and read in a template with expressions like new_order["drink"]. section 11.0 section 11.0

For a tidy order, use keys that describe each choice:

  • "name" for the customer’s entered name. section 7.0
  • "drink", "flavor", and "topping" for menu selections. section 6.0
  • The same keys when printing the order, so every value has a clear label. section 11.0

How A Pythonic Adventure: From Python basics to a working web app covers it

How deep it goes
Defines key-value storage and demonstrates empty creation, assignment, return, form collection, and template lookup without discussing dictionary internals.
Examples
Uses a coffee order with name, drink, flavor, and topping, plus submitted web-form values rendered on a page.
What it assumes
Builds from empty curly-brace dictionaries while relying on earlier familiarity with variables, input(), functions, loops, and web forms.
What you can do after
Build it from scratch as an order dictionary, populate it from input or a form, return it, and retrieve values by key.
How it connects
Connects dictionaries with functions, input(), loops, Flask request.form, templates, lists, and JSON saving.

Also explained in

Compare how 42 books explain Map →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of A Pythonic Adventure: From Python basics to a working web app. The section links point to the parts of the book it draws on.