liveBook · Manning

Key

As explained in Python Workout, Second Edition

A dictionary key is a hashable value used to identify and retrieve a value in a Python dictionary.

A dictionary key is the first item in a key–value pair: a hashable value used to locate its associated value. Keys may be integers, strings, or other hashable types, and each key is unique. section 5.0 section 5.20

Use keys to:

  • Retrieve values with dictionary lookup or test whether a key exists using in. section 5.5
  • Represent name–value data such as usernames, hostnames, configuration options, or product prices. section 5.0
  • Iterate through dictionary keys or safely retrieve a possibly missing key with dict.get. section 5.20

Menu lookup

Store product names as keys and prices as values. After stripping input, check whether the order is a key before retrieving its price. Matching is case-sensitive; casefold can normalize user input before comparison. section 5.5

Keys must be hashable, while values may be any objects, including dictionaries. Square-bracket access ultimately invokes __getitem__; custom dictionary subclasses can reinterpret keys, but guessing between key types is discouraged. section 5.20 section 10.22

How Python Workout, Second Edition covers it

How deep it goes
Defines hashability, uniqueness, lookup, iteration, and case sensitivity, then connects square brackets to __getitem__.
Examples
Demonstrates menu prices, user-entered product names, case normalization, and a FlexibleDict subclass.
What you can do after
Apply keys to build lookups, test membership, retrieve values, iterate dictionaries, and handle missing entries.
How it connects
Connects keys with dictionaries, values, hash functions, casefold, dict.get, and __getitem__.

Also explained in

Compare how 10 books explain key →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Workout, Second Edition. The section links point to the parts of the book it draws on.