When a program needs to associate a username with an ID, a hostname with an IP address, or an option with its setting, use a name-value pair: an identifying key points to its corresponding value. Python dictionaries store these associations, allowing retrieval by meaningful keys instead of integer indexes. section 4.0
A dictionary makes name-value pairs useful for several common jobs:
- Store a small record, such as configuration options, file information, or user preferences. section 4.0
- Keep related values together instead of scattering them across separate variables, then pass them through a program as one object. section 4.0
- Accumulate results over time, such as word counts or other measurements keyed by descriptive names. section 5.12
Looking up a value by name
A menu can associate product names with prices: menu = {'coffee': 3, 'sandwich': 8} order = input('Order: ').strip() if order in menu: print(f'Total: ${menu[order]}') else: print('That product is not in stock.') The key is the product name and the value is its price. The in operator checks keys, while menu[order] retrieves the associated value. This is more natural than searching a list of lists or tuples. section 4.4
Python implements dictionary lookup with hashing. A hash function maps a key to a storage location, enabling fast key lookup and ensuring key uniqueness; it does not provide the same guarantee for finding values. The model omits implementation details, including changes associated with Python 3.6. section 4.1
An initialized counts dictionary provides a compact visual example of descriptive names associated with values before a file-processing task updates them. section 5.12










