Dictionaries in Python#
đ Data Analysis Using Python đ Strings & Data Structures Lesson 023
â Previous ¡ Next ✠¡ â Section ¡ â Hub
Important
⨠AI-generated content. This page was written with the assistance of an AI language model and is provided as a learning aid. Despite careful review, it may still contain mistakes, omissions, or out-of-date information. Whether you are new to the topic, a team lead, or a senior practitioner, treat it as a starting point rather than an authoritative reference: read it critically and independently verify anything you act on (code, commands, figures, and factual claims) against official documentation and primary sources before relying on it.
Looking values up by key#
Lists and tuples hold values in order, accessed by position. But often you want to look a value up by a meaningful key â a customerâs name, a product code, a field label â rather than a numeric position. The dictionary is Pythonâs key-value structure, and it is one of the most important for data work. This lesson covers dictionaries.
What a dictionary is#
A dictionary stores key-value pairs â each value is associated with a key that identifies it, written with curly braces:
customer = {
"name": "Jane Smith",
"region": "North",
"sales": 1000,
}
Values are looked up by key, not position:
customer["name"] # "Jane Smith"
customer["sales"] # 1000
Where a list answers âwhat is at position 2?â, a dictionary answers âwhat is the value for ânameâ?â â access by meaningful key rather than numeric index. This makes dictionaries ideal for representing records with named fields.
Modifying dictionaries#
Dictionaries are mutable â pairs can be added, changed, and removed:
customer["email"] = "jane@example.com" # add a new key-value pair
customer["sales"] = 1200 # update an existing value
del customer["region"] # remove a pair
Assigning to a key either adds it (if new) or updates it (if it exists); del removes a
pair. Dictionaries grow and change like lists, but keyed rather than ordered.
Why dictionaries matter#
Dictionaries are fundamental to data work for several reasons. They represent records
naturally â a row of data as field-name-to-value pairs ({"name": ..., "sales": ...}),
which is exactly how structured data is often held. They enable fast lookup by key (far
faster than searching a list). And they are the structure behind much of Pythonâs data
ecosystem â JSON data is dictionaries, pandas DataFrames can be built from them, and
configuration and mappings are dictionaries. Understanding key-value access is understanding
a core pattern of representing and retrieving structured data.
The caveat#
Dictionaries have specific rules and pitfalls. Keys must be unique â assigning to an
existing key overwrites its value rather than adding a second, so duplicate keys silently
lose data. Keys must be immutable â strings, numbers, and tuples can be keys, but lists
cannot (their mutability would break the dictionaryâs lookup), which is one reason tuples
exist. And the classic error: accessing a key that does not exist (customer["phone"]
when there is no phone) raises a KeyError and stops the program â a frequent bug when
data may be missing a field. The next lesson covers handling this and other advanced
dictionary usage safely. Keys unique and immutable, and access defensively.
Hint
See also
Source article Adapted (context, re-expressed) in our own words from: https://insightful-data-lab.com/2023/12/06/dictionaries-in-python/ (insightful-data-lab.com).