Key-Value Storage for Fast Lookup¶

Dictionaries let you find values by name instead of position¶
Lists and tuples work well when order matters. Dictionaries solve a different problem: you want to jump directly to information using a label such as a product ID, feature name, or customer key. That makes them one of the most practical structures in analytics, APIs, and business applications.
Core Explanation¶
A dictionary is often the best choice when you want to connect identifiers to values, such as:
product IDs to prices
employee IDs to names
feature names to scores
metric labels to current values
Continuity from tuples
Tuples grouped fixed values by position. Dictionaries group related values by label. Use a dictionary when the name of the field matters more than its numeric position.
prices = {"P1": 25, "P2": 40, "P3": 18}
print(prices["P2"])
prices["P4"] = 33
print(prices)Worked Example: lookup by label instead of index¶
The code above shows the main dictionary advantage: you do not need to remember that a price sits at position 1 or 2 in a sequence. You ask for the value by its key, such as "P2".
Exercises¶
Create a dictionary of employee names and departments.
Add one new key-value pair.
Explain why a dictionary is often better than a list for lookup tasks.
Give one example of a business metric or configuration setting that should be stored by label rather than by position.
Hint
If the question sounds like “find the value for this name, ID, or label,” a dictionary is usually a better fit than a list or tuple.
Quick Summary
dictionaries store key-value pairs
dictionaries are strong for lookup and mapping tasks
keys should be unique within the dictionary
dictionaries are often easier to read when field names matter
Practice Lab¶
Expected output
{'P1': 25, 'P2': 40, 'P3': 18, 'P4': 33}Expected output
25
['notebook', 'pen']Dictionary Methods¶
These examples separate the main dictionary methods so each lookup and update pattern is easier to understand.
get()¶
Use get() to read a value safely by key.
keys()¶
Use keys() to inspect the available labels in the dictionary.
values()¶
Use values() when you want the stored values without the keys.
items()¶
Use items() to work with keys and values together.
update()¶
Use update() to add or overwrite multiple entries at once.
pop()¶
Use pop() to remove a key and return its value.
setdefault()¶
Use setdefault() to create a key only if it does not already exist.
copy()¶
Use copy() when you want a separate dictionary before editing it.
Guided Practice¶
Which method retrieves a value safely by key?¶
What does `inventory.keys()` provide?¶
Key Takeaway¶
Dictionaries are the go-to structure when names, IDs, or labels matter more than position. They make lookup explicit and keep related information easier to read and maintain.
Deeper Python Dictionary Patterns¶
Adapted for this course from the Python Tutorial data structures chapter.
This section extends the basic mapping idea into patterns you will use often in real Python code:
creating dictionaries in different ways
adding and deleting entries
iterating over keys and values
membership checks
dictionary comprehensions
How to read this section
Focus on what each pattern helps you express: safe lookup, readable updates, and fast access by label.
# Basic dictionary operations
tel = {'jack': 4098, 'sape': 4139}
tel['guido'] = 4127
print(tel)
del tel['sape']
tel['irv'] = 4127
print(tel)
print('Keys:', list(tel.keys()))
print('Sorted keys:', sorted(tel.keys()))
print('Contains guido?', 'guido' in tel)
print('Contains jack?', 'jack' not in tel)
# dict() constructor with keyword arguments
person = dict(sape=4139, guido=4127, jack=4098)
print(person)
# Dictionary comprehension
squares = {x: x**2 for x in (2, 4, 6)}
print('Squares map:', squares)