For any data scientist or software engineer, the ability to efficiently map relationships between data points is a non-negotiable skill. If you are parsing complex JSON responses from an API, aggregating statistics from a massive dataset, or simply configuring application settings, then a dictionary is arguably Python's most powerful tool. It drives clean, readable, and highly optimized data manipulation.
While anyone can look up a value in a dictionary, real expertise shows when you know how to apply its methods to your data workflows and unlock advanced patterns.
In this article, we’ll look at hash tables that make dictionaries so fast, essential dictionary methods, error handling strategies, and performance optimization techniques.
If you're new to dictionaries, I recommend reading our foundational Python Dictionary tutorial as a good starting point.
What Are Python Dictionaries?
Python dictionaries are a built-in data structure designed for fast, flexible lookups. They allow you to store and retrieve values using meaningful keys rather than numeric positions, which makes them ideal for representing structured, real-world data. Let’s look at their core structure and properties.
Core dictionary architecture and hashing
Before we get into the details of Python’s dictionary methods, it helps to understand how dictionaries are built on hash tables. Many of the errors you’ll run into, like TypeError: unhashable type, come straight from the way this structure works.
At a structural level, a Python dictionary implements a hash table. This architectural choice is what gives the dictionary its speed and versatility. When you define a dictionary, you are essentially creating a sparse array, often called a bucket array.
When you insert a key-value pair, Python passes the key through a hash function. This function calculates a unique integer (the hash) that determines the specific index in the bucket array where the value will be stored.
Because of this design:
-
Keys must be hashable, which usually means they must be of an immutable type (e.g., str, int, tuple)
-
Values can be mutable, including lists, other dictionaries, or custom objects
-
Lookups, insertions, and deletions run in average amortized O(1) time
The following example shows a few valid keys, but also demonstrates that a list is not accepted as dictionary key:
user_data = {
"name": "Alice",
42: [1, 2, 3],
(10, 20): {"nested": True}
}
print(type(user_data), "valid dict")
try:
invalid_dict = {[1, 2]: "value"}
except TypeError as e:
print(f"Error: {e}")
<class 'dict'> valid dict
Error: unhashable type: 'list'
This structure is important for performance. While searching for an item in a list requires iterating through elements one by one, an O(n) operation, retrieving a value from a dictionary is an O(1) operation on average.
This means looking up a user ID in a dataset of one million users takes roughly the same amount of time as looking it up in a dataset of ten users. Understanding the differences between data types in Python is key to choosing the right structure for your use case.

Evolution of order and properties
One of the most significant changes in Python's history occurred in version 3.7. Before this, dictionaries were considered unordered collections, and iterating over them could produce keys in seemingly random sequences. If you printed a dictionary, the items might appear in a different order than you inserted them, depending on the hash values and the internal array history.
However, starting with Python 3.6, dictionaries began preserving insertion order as an implementation detail in CPython. Then, beginning with Python 3.7 (and officially guaranteed in the language specification), dictionaries preserve insertion order.
This shift from unordered to ordered mappings has a few important implications for modern Python development. For instance, JSON serialization now produces predictable output, which makes debugging easier and ensures data reproducibility across different runs.
If you're working with data pipelines where order matters, such as processing time-series events or maintaining configuration hierarchies, this guarantee eliminates an entire class of subtle bugs. Next, let’s see how to create a dictionary.
Creating a Python Dictionary
While creating a dictionary seems straightforward, the method you choose can impact both the readability and performance of your code. Python provides multiple ways for initialization, which range from simple literals to advanced programmatic generation techniques for data science workflows. Let’s look at the most important ways.
Curly brace literals
The most common and preferred way to create a dictionary is using curly brace syntax {}. This literal notation is not only more readable but also faster than alternative methods. Python can optimize the bytecode construction directly without the overhead of a function call. Below is the code showing a dictionary:
user_profile = {
"name": "Alice",
"role": "Data Scientist",
"active": True
}
user_profile
{'name': 'Alice', 'role': 'Data Scientist', 'active': True}
dict() constructor
However, the dict() constructor is indispensable in a few scenarios. It acts as a type converter, allowing you to build dictionaries from sequences of tuples or keyword arguments. It is particularly useful in these specific cases:
- Keys are valid Python identifiers, but you want to avoid quoting strings
- You need to transforming data structures like lists of paired values
config = dict(host="localhost", port=8080, debug=True)
print(config)
pairs = [("a", 1), ("b", 2), ("c", 3)]
lookup_table = dict(pairs)
print(lookup_table)
{'host': 'localhost', 'port': 8080, 'debug': True}
{'a': 1, 'b': 2, 'c': 3}
For more complex dictionary creation scenarios, dictionary comprehensions offer a concise and efficient way to filter, transform, or generate key-value pairs programmatically. It’s an essential technique for any data practitioner who needs to process and reshape data dynamically.
Comprehensions are vital for tasks like:
- Inverting a dictionary
- Filtering out null values from a dataset

Let’s see how to create a dictionary comprehension below:
squares = {x: x**2 for x in range(5)}
print(squares)
raw_data = {"a": 10, "b": None, "c": 5}
clean_data = {k: v for k, v in raw_data.items() if v is not None}
print(clean_data)
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
{'a': 10, 'c': 5}
If you want to dive deeper, I recommend you go through our Python Dictionary Comprehension tutorial.
dict.fromkeys()
Another useful Python dictionary method for initialization is dict.fromkeys(). This method creates a new dictionary with specified keys and a single value. It is often used to initialize counters or status flags.
categories = ["electronics", "clothing", "food", "books"]
inventory = dict.fromkeys(categories, 0)
print(inventory)
user_fields = ["email", "phone", "address", "company"]
user_profile = dict.fromkeys(user_fields)
print(user_profile)
{'electronics': 0, 'clothing': 0, 'food': 0, 'books': 0}
{'email': None, 'phone': None, 'address': None, 'company': None}
When using .fromkeys() with mutable objects like lists or dictionaries, all keys will reference the same object in memory. This creates a “shared reference” pitfall that can lead to unexpected behavior. Let’s see this with an example:
categories = ["A", "B", "C"]
wrong_way = dict.fromkeys(categories, [])
wrong_way["A"].append(1)
print(wrong_way)
right_way = {cat: [] for cat in categories}
right_way["A"].append(1)
print(right_way)
{'A': [1], 'B': [1], 'C': [1]}
{'A': [1], 'B': [], 'C': []}
We can see that the same value was shared by all keys in the first case. To avoid this, we need to use a dictionary comprehension for independent lists.
Python Dictionary Methods For Access and Modification
Once a dictionary is created, interacting with the data stored inside is one of the most common daily programming tasks. Let’s look at some of these ways.
Accessing and retrieving values
There are a few different ways to access values.
Bracket notation
The most direct way to retrieve a value from a dictionary is through bracket notation d[key], which returns the associated value if the key exists. This approach is ideal when you're confident the key is present in your dictionary. Below is the code to do it:
product = {
"name": "Laptop",
"price": 1299.99,
"stock": 45,
"category": "Electronics"
}
print(product["name"])
print(product["price"])
try:
print(product["manufacturer"])
except KeyError as e:
print(f"Key not found: {e}")
Laptop
1299.99
Key not found: 'manufacturer'
.get() method
For safer retrieval when key existence is uncertain, the .get() method provides an elegant solution. It returns None (or a specified default value) instead of raising an exception if the key doesn't exist.
manufacturer = product.get("manufacturer")
print(manufacturer)
warranty = product.get("warranty", "No warranty information")
print(warranty)
customer_data = {"name": "John Doe", "email": "[email protected]"}
phone = customer_data.get("phone", "Not provided")
print(phone)
address = customer_data.get("address", "Not provided")
print(address)
None
No warranty information
Not provided
Not provided
.setdefault() method
The .setdefault() method combines retrieval and insertion in a single operation. It retrieves a value if the key exists, or inserts a default value and returns it if the key is missing, which is perfect for accumulation patterns.
page_visits = {}
count = page_visits.setdefault("home", 0)
print(count)
page_visits["home"] += 1
count = page_visits.setdefault("home", 0)
print(count)
inventory = [
("apple", "fruit"),
("carrot", "vegetable"),
("banana", "fruit"),
("broccoli", "vegetable")
]
grouped = {}
for item, category in inventory:
grouped.setdefault(category, []).append(item)
print(grouped)
0
1
{'fruit': ['apple', 'banana'], 'vegetable': ['carrot', 'broccoli']}
Modifying dictionaries
Dictionaries are dynamic; you will often need to add or remove data as your program executes.
Adding key-value pairs with the .update() method
Adding a single pair is as simple as assignment (d['new'] = 1), but for bulk operations, the .update() method is superior. It accepts another dictionary or an iterable of key-value pairs, and merges them into the existing object.
Let’s see how to use the .update() method:
user = {"username": "alice_2024", "role": "analyst"}
user["email"] = "[email protected]"
user["role"] = "senior_analyst"
user.update({"department": "Analytics", "level": 3})
print(user)
additional_info = [("projects", 12), ("rating", 4.8)]
user.update(additional_info)
print(user)
user.update(active=True, certified=True)
print(user)
{'username': 'alice_2024', 'role': 'senior_analyst', 'email': '[email protected]', 'department': 'Analytics', 'level': 3}
{'username': 'alice_2024', 'role': 'senior_analyst', 'email': '[email protected]', 'department': 'Analytics', 'level': 3, 'projects': 12, 'rating': 4.8}
{'username': 'alice_2024', 'role': 'senior_analyst', 'email': '[email protected]', 'department': 'Analytics', 'level': 3, 'projects': 12, 'rating': 4.8, 'active': True, 'certified': True}
For a detailed walkthrough on adding items, I recommend you go through this guide on Python Dictionary Append.
Removing items from a dictionary
Python offers three distinct methods for removing dictionary entries, each with different behavior and use cases:
-
.pop(key): Removes the key and returns its value. This is useful when you need to use the data one last time before deleting it.
-
.popitem(): Removes and returns the last inserted key-value pair (LIFO). This is a direct benefit of the ordered nature of modern dictionaries.
-
del d[key]: purely deletes the key. It does not return the value and is slightly faster if the return value is not needed.
Let’s see examples of these methods:
.pop() method:
scores = {"Alice": 95, "Bob": 87, "Carol": 92, "David": 78}
alice_score = scores.pop("Alice")
print(alice_score)
print(scores)
95
{'Bob': 87, 'Carol': 92, 'David': 78}
.popitem() method:
scores = {"Alice": 95, "Bob": 87, "Carol": 92, "David": 78}
last_item = scores.popitem()
print(last_item)
print(scores)
('David', 78)
{'Alice': 95, 'Bob': 87, 'Carol': 92}
del:
scores = {"Alice": 95, "Bob": 87, "Carol": 92, "David": 78}
del scores["Bob"]
print(scores)
{'Alice': 95, 'Carol': 92, 'David': 78}
Emptying a dictionary with the .clear() method
The .clear() method empties the entire dictionary, leaving you with an empty {} object. This is distinct from deleting the variable itself. The object remains in memory, just empty. Let’s see how this method works:
scores = {"Alice": 95, "Bob": 87, "Carol": 92, "David": 78}
scores.clear()
print(scores)
print(type(scores))
The distinction between these methods matters. Use .pop() when you need the removed value, .popitem() for stack-like behavior, del for simple removal, and .clear() to reset a dictionary while preserving its identity.
Views and iteration
In older versions of Python 2, methods like .keys() returned a static list. In the current versions of Python (3.x), these return view objects. Views are dynamic windows into the dictionary. If the dictionary changes, the view reflects those changes instantly without needing to be re-called.
Iterating through .keys(), .values(), and .items()
You can iterate through keys (.keys()), values (.values()), or both simultaneously using .items(). Let’s see these methods with an example:
experiment = {
"model": "RandomForest",
"accuracy": 0.94,
"precision": 0.91,
"recall": 0.89
}
print(experiment.keys())
print(experiment.values())
for metric, value in experiment.items():
if isinstance(value, float):
print(f"{metric}: {value:.2%}")
dict_keys(['model', 'accuracy', 'precision', 'recall'])
dict_values(['RandomForest', 0.94, 0.91, 0.89])
accuracy: 94.00%
precision: 91.00%
recall: 89.00%
The dynamic nature of view objects means they automatically reflect changes made to the dictionary after the view is created. Let’s see an example:
metrics = {"MAE": 0.23, "RMSE": 0.45}
keys_view = metrics.keys()
print(keys_view)
metrics["R2"] = 0.87
print(keys_view)
dict_keys(['MAE', 'RMSE'])
dict_keys(['MAE', 'RMSE', 'R2'])
Intersections and unions
A powerful feature of view objects is that they support set operations. You can perform them directly on key views to compare two dictionaries efficiently using these operators:
Let’s see an example:
dict1 = {"a": 1, "b": 2, "c": 3}
dict2 = {"b": 20, "c": 30, "d": 4}
common_keys = dict1.keys() & dict2.keys()
print(common_keys)
all_keys = dict1.keys() | dict2.keys()
print(all_keys)
unique_to_dict1 = dict1.keys() - dict2.keys()
print(unique_to_dict1)
exclusive_keys = dict1.keys() ^ dict2.keys()
print(exclusive_keys)
{'b', 'c'}
{'b', 'c', 'd', 'a'}
{'a'}
{'d', 'a'}
These view-based set operations are far more memory-efficient than converting dictionaries to sets explicitly, especially when working with large datasets.
Avoiding Errors In Python Dictionary Methods
Because dictionaries are often used as the primary interface for external data, such as JSON payloads from APIs or configuration files, they are a common source of runtime errors. Writing robust code requires more than just knowing how to access data. It requires knowing how to handle its absence.
The most common dictionary error is KeyError, which occurs when attempting to access a key that doesn't exist. Python offers two philosophical approaches to handling this:
- EAFP (Easier to Ask Forgiveness than Permission)
- LBYL (Look Before You Leap)
EAFP: Managing KeyError and exceptions
The EAFP pattern is considered more Pythonic and often performs better when keys usually exist, as it avoids redundant checks. It uses try-except blocks to handle errors after they occur, assuming that operations will usually succeed. Let’s see how this works:
user_data = {"username": "data_analyst", "email": "[email protected]"}
try:
phone = user_data["phone"]
print(f"Phone: {phone}")
except KeyError:
print("Phone number not available")
phone = None
config = {"host": "localhost", "port": 5432}
try:
database = config["database"]
except KeyError:
print("Warning: Database not specified, using default")
database = "default_db"
config["database"] = database
Phone number not available
Warning: Database not specified, using default
However, there are scenarios where failing loudly, allowing the KeyError to propagate, is actually preferable to silent failures. In the LBYL approach, you explicitly check for the key's existence before accessing it. Let’s see an example:
required_config = {"api_key": "secret123", "endpoint": "api.example.com"}
def initialize_api(config):
api_key = config["api_key"]
endpoint = config["endpoint"]
timeout = config.get("timeout", 30)
return {"key": api_key, "endpoint": endpoint, "timeout": timeout}
Invoking this function with will raise KeyError if any key in the dictionary is missing, which is correct behavior because it is better to fail during initialization than silently during production.
Explicit error handling
When processing data from external sources like APIs or user input, explicit error handling becomes important. Let’s see how to do it with an example:
def extract_user_info(api_response):
"""Extract user information with comprehensive error handling."""
user_info = {}
try:
user_info["id"] = api_response["user"]["id"]
user_info["name"] = api_response["user"]["profile"]["name"]
except KeyError as e:
print(f"Missing required field in API response: {e}")
return None
user_info["email"] = api_response.get("user", {}).get("contact", {}).get("email", "N/A")
user_info["verified"] = api_response.get("user", {}).get("verified", False)
return user_info
response = {
"user": {
"id": 12345,
"profile": {"name": "Jane Smith"},
"verified": True
}
}
user = extract_user_info(response)
print(user)
{'id': 12345, 'name': 'Jane Smith', 'email': 'N/A', 'verified': True}
Understanding when to use .get() versus bracket notation versus try-except is important.
LBYL: Proactive membership testing
The LBYL approach uses conditional statements using the in and not in operators to check for key existence before attempting access. This pattern is clearer when dealing with conditional logic or when you need to take different actions based on key presence. Let’s see an example of this:
student_grades = {
"Alice": 95,
"Bob": 87,
"Carol": 92
}
student_name = "David"
if student_name in student_grades:
print(f"{student_name}'s grade: {student_grades[student_name]}")
else:
print(f"No grade recorded for {student_name}")
if "David" not in student_grades:
student_grades["David"] = 0
print("New student added to grading system")
required_fields = ["name", "email", "department"]
employee_record = {"name": "John Doe", "email": "[email protected]"}
missing_fields = [field for field in required_fields if field not in employee_record]
if missing_fields:
print(f"Error: Missing required fields: {missing_fields}")
else:
print("All required fields present")
No grade recorded for David
New student added to grading system
Error: Missing required fields: ['department']
When validating dictionaries derived from external sources such as JSON from APIs, CSV files, or user input, proactive membership testing provides clear, readable validation logic. Let’s see this with an example:
def validate_product_data(product):
"""Validate product dictionary has all required fields."""
required = ["id", "name", "price", "category"]
optional = ["description", "stock", "manufacturer"]
for field in required:
if field not in product:
raise ValueError(f"Missing required field: {field}")
if "price" in product and not isinstance(product["price"], (int, float)):
raise TypeError("Price must be a number")
if "stock" in product and product["stock"] < 0:
raise ValueError("Stock cannot be negative")
return True
product_from_api = {
"id": 101,
"name": "Wireless Mouse",
"price": 29.99,
"category": "Electronics",
"stock": 150
}
try:
if validate_product_data(product_from_api):
print("Product data validated successfully")
except (ValueError, TypeError) as e:
print(f"Validation failed: {e}")
Product data validated successfully
Choosing the right approach
The choice between EAFP and LBYL often depends on your use case. Use EAFP when operations typically succeed and exceptions are rare. Use LBYL when you need explicit branching logic or when validating input before expensive operations.
Before iterating over a dictionary derived from an external source, it is best practice to:
- Validate early
- Check all required keys at once
- Raise specific and informative exceptions
- Clearly separate required fields from optional ones
- Validate more than just presence when needed
- Prefer explicit failure over silent fallbacks
By following these practices, you can make your code much more robust when dealing with unpredictable or incomplete data from APIs, user input, or configuration files.
For a more deeper look into writing resilient Python code, I recommend taking our course on Writing Efficient Python Code.
Advanced Python Dictionary Methods
As your data science projects grow in complexity, you’ll often need to combine, copy, and transform dictionaries. Learning these advanced operations enables you to manipulate data structures efficiently while avoiding subtle bugs that can derail pipelines. Let’s see some of these methods.
Merging operators
Python 3.9 introduced elegant union operators for merging dictionaries:
These operators provide clean, readable syntax for combining data from multiple sources. Let’s see an example:
defaults = {"theme": "light", "language": "en", "notifications": True}
user_prefs = {"theme": "dark", "font_size": 14}
final_config = defaults | user_prefs
print(final_config)
settings = {"auto_save": True, "theme": "light"}
settings |= {"theme": "dark", "font_size": 12}
print(settings)
{'theme': 'dark', 'language': 'en', 'notifications': True, 'font_size': 14}
{'auto_save': True, 'theme': 'dark', 'font_size': 12}
For Python versions before 3.9, the double-star unpacking method (**) provides similar functionality:
base_config = {"host": "localhost", "port": 5432, "ssl": False}
override_config = {"port": 5433, "ssl": True, "timeout": 30}
merged = {**base_config, **override_config}
print(merged)
db_config = {"database": "analytics"}
auth_config = {"username": "admin", "password": "secret"}
pool_config = {"pool_size": 10, "max_overflow": 20}
complete_config = {**db_config, **auth_config, **pool_config}
print(complete_config)
{'host': 'localhost', 'port': 5433, 'ssl': True, 'timeout': 30}
{'database': 'analytics', 'username': 'admin', 'password': 'secret', 'pool_size': 10, 'max_overflow': 20}
Collision precedence
Across all merging techniques, the rule is simple: last seen wins. The value from the right-hand dictionary overwrites the left-hand one if keys collide:
dict1 = {"a": 1, "b": 2, "c": 3}
dict2 = {"b": 20, "c": 30, "d": 4}
dict3 = {"c": 300, "e": 5}
result = dict1 | dict2 | dict3
print(result)
result2 = {**dict1, **dict2, **dict3}
print(result2)
result3 = dict3 | dict2 | dict1
print(result3)
{'a': 1, 'b': 20, 'c': 300, 'd': 4, 'e': 5}
{'a': 1, 'b': 20, 'c': 300, 'd': 4, 'e': 5}
{'c': 3, 'e': 5, 'b': 2, 'd': 4, 'a': 1}
This behavior is consistent whether you use |, |=, unpacking, or the .update() method. Order matters, making it especially useful for layered configuration management (e.g., system defaults < environment configs < user overrides).

Copying mechanisms
One of the most dangerous pitfalls is misunderstanding how Python handles variable assignments.
Reference assignments create aliases, not copies
dict_a = dict_b does not create a copy. It creates a reference (an alias). Modifying one modifies the other. The following example illustrates the concept:
original = {"name": "Dataset_v1", "records": 1000}
alias = original
alias["records"] = 2000
print(original)
print(alias is original)
{'name': 'Dataset_v1', 'records': 2000}
True
As we can see, there is only one dictionary and both original and alias reference it. Accordingly, when the value for the key records is set to 2000, the change can only apply to this one dictionary.
Shallow copies with .copy() or .dict()
A shallow copy (.copy() or dict()) creates a new dictionary object, but nested mutable objects remain shared references.
original = {
"name": "Experiment_A",
"parameters": {"learning_rate": 0.01, "epochs": 100},
"results": [0.85, 0.89, 0.92]
}
shallow = original.copy()
shallow["name"] = "Experiment_B"
print(original["name"])
print(shallow["name"])
shallow["parameters"]["learning_rate"] = 0.001
print(original["parameters"]["learning_rate"])
shallow["results"].append(0.94)
print(original["results"])
For any data scientist or software engineer, the ability to efficiently map relationships between data points is a non-negotiable skill. If you are parsing complex JSON responses from an API, aggregating statistics from a massive dataset, or simply configuring application settings, then a dictionary is arguably Python's most powerful tool. It drives clean, readable, and highly optimized data manipulation.
While anyone can look up a value in a dictionary, real expertise shows when you know how to apply its methods to your data workflows and unlock advanced patterns.
In this article, we’ll look at hash tables that make dictionaries so fast, essential dictionary methods, error handling strategies, and performance optimization techniques.
If you're new to dictionaries, I recommend reading our foundational Python Dictionary tutorial as a good starting point.
What Are Python Dictionaries?
Python dictionaries are a built-in data structure designed for fast, flexible lookups. They allow you to store and retrieve values using meaningful keys rather than numeric positions, which makes them ideal for representing structured, real-world data. Let’s look at their core structure and properties.
Core dictionary architecture and hashing
Before we get into the details of Python’s dictionary methods, it helps to understand how dictionaries are built on hash tables. Many of the errors you’ll run into, like
TypeError: unhashable type, come straight from the way this structure works.At a structural level, a Python dictionary implements a hash table. This architectural choice is what gives the dictionary its speed and versatility. When you define a dictionary, you are essentially creating a sparse array, often called a bucket array.
When you insert a key-value pair, Python passes the key through a hash function. This function calculates a unique integer (the hash) that determines the specific index in the bucket array where the value will be stored.
Because of this design:
Keys must be hashable, which usually means they must be of an immutable type (e.g.,
str,int,tuple)Values can be mutable, including lists, other dictionaries, or custom objects
Lookups, insertions, and deletions run in average amortized O(1) time
The following example shows a few valid keys, but also demonstrates that a list is not accepted as dictionary key:
This structure is important for performance. While searching for an item in a list requires iterating through elements one by one, an O(n) operation, retrieving a value from a dictionary is an O(1) operation on average.
This means looking up a user ID in a dataset of one million users takes roughly the same amount of time as looking it up in a dataset of ten users. Understanding the differences between data types in Python is key to choosing the right structure for your use case.
Evolution of order and properties
One of the most significant changes in Python's history occurred in version 3.7. Before this, dictionaries were considered unordered collections, and iterating over them could produce keys in seemingly random sequences. If you printed a dictionary, the items might appear in a different order than you inserted them, depending on the hash values and the internal array history.
However, starting with Python 3.6, dictionaries began preserving insertion order as an implementation detail in CPython. Then, beginning with Python 3.7 (and officially guaranteed in the language specification), dictionaries preserve insertion order.
This shift from unordered to ordered mappings has a few important implications for modern Python development. For instance, JSON serialization now produces predictable output, which makes debugging easier and ensures data reproducibility across different runs.
If you're working with data pipelines where order matters, such as processing time-series events or maintaining configuration hierarchies, this guarantee eliminates an entire class of subtle bugs. Next, let’s see how to create a dictionary.
Creating a Python Dictionary
While creating a dictionary seems straightforward, the method you choose can impact both the readability and performance of your code. Python provides multiple ways for initialization, which range from simple literals to advanced programmatic generation techniques for data science workflows. Let’s look at the most important ways.
Curly brace literals
The most common and preferred way to create a dictionary is using curly brace syntax
{}. This literal notation is not only more readable but also faster than alternative methods. Python can optimize the bytecode construction directly without the overhead of a function call. Below is the code showing a dictionary:dict() constructor
However, the
dict()constructor is indispensable in a few scenarios. It acts as a type converter, allowing you to build dictionaries from sequences of tuples or keyword arguments. It is particularly useful in these specific cases:Dictionary comprehensions
For more complex dictionary creation scenarios, dictionary comprehensions offer a concise and efficient way to filter, transform, or generate key-value pairs programmatically. It’s an essential technique for any data practitioner who needs to process and reshape data dynamically.
Comprehensions are vital for tasks like:
Let’s see how to create a dictionary comprehension below:
If you want to dive deeper, I recommend you go through our Python Dictionary Comprehension tutorial.
dict.fromkeys()
Another useful Python dictionary method for initialization is
dict.fromkeys(). This method creates a new dictionary with specified keys and a single value. It is often used to initialize counters or status flags.When using
.fromkeys()with mutable objects like lists or dictionaries, all keys will reference the same object in memory. This creates a “shared reference” pitfall that can lead to unexpected behavior. Let’s see this with an example:We can see that the same value was shared by all keys in the first case. To avoid this, we need to use a dictionary comprehension for independent lists.
Python Dictionary Methods For Access and Modification
Once a dictionary is created, interacting with the data stored inside is one of the most common daily programming tasks. Let’s look at some of these ways.
Accessing and retrieving values
There are a few different ways to access values.
Bracket notation
The most direct way to retrieve a value from a dictionary is through bracket notation
d[key], which returns the associated value if the key exists. This approach is ideal when you're confident the key is present in your dictionary. Below is the code to do it:.get() method
For safer retrieval when key existence is uncertain, the
.get()method provides an elegant solution. It returnsNone(or a specified default value) instead of raising an exception if the key doesn't exist..setdefault() method
The
.setdefault()method combines retrieval and insertion in a single operation. It retrieves a value if the key exists, or inserts a default value and returns it if the key is missing, which is perfect for accumulation patterns.Modifying dictionaries
Dictionaries are dynamic; you will often need to add or remove data as your program executes.
Adding key-value pairs with the .update() method
Adding a single pair is as simple as assignment (
d['new'] = 1), but for bulk operations, the.update()method is superior. It accepts another dictionary or an iterable of key-value pairs, and merges them into the existing object.Let’s see how to use the
.update()method:For a detailed walkthrough on adding items, I recommend you go through this guide on Python Dictionary Append.
Removing items from a dictionary
Python offers three distinct methods for removing dictionary entries, each with different behavior and use cases:
.pop(key): Removes the key and returns its value. This is useful when you need to use the data one last time before deleting it..popitem(): Removes and returns the last inserted key-value pair (LIFO). This is a direct benefit of the ordered nature of modern dictionaries.del d[key]: purely deletes the key. It does not return the value and is slightly faster if the return value is not needed.Let’s see examples of these methods:
.pop()method:.popitem()method:del:Emptying a dictionary with the .clear() method
The
.clear()method empties the entire dictionary, leaving you with an empty{}object. This is distinct from deleting the variable itself. The object remains in memory, just empty. Let’s see how this method works:The distinction between these methods matters. Use
.pop()when you need the removed value,.popitem()for stack-like behavior,delfor simple removal, and.clear()to reset a dictionary while preserving its identity.Views and iteration
In older versions of Python 2, methods like
.keys()returned a static list. In the current versions of Python (3.x), these return view objects. Views are dynamic windows into the dictionary. If the dictionary changes, the view reflects those changes instantly without needing to be re-called.Iterating through .keys(), .values(), and .items()
You can iterate through keys (
.keys()), values (.values()), or both simultaneously using.items(). Let’s see these methods with an example:The dynamic nature of view objects means they automatically reflect changes made to the dictionary after the view is created. Let’s see an example:
Intersections and unions
A powerful feature of view objects is that they support set operations. You can perform them directly on key views to compare two dictionaries efficiently using these operators:
Intersection:
&Union:
|Difference:
-Symmetric difference/XOR:
^Let’s see an example:
These view-based set operations are far more memory-efficient than converting dictionaries to sets explicitly, especially when working with large datasets.
Avoiding Errors In Python Dictionary Methods
Because dictionaries are often used as the primary interface for external data, such as JSON payloads from APIs or configuration files, they are a common source of runtime errors. Writing robust code requires more than just knowing how to access data. It requires knowing how to handle its absence.
The most common dictionary error is
KeyError, which occurs when attempting to access a key that doesn't exist. Python offers two philosophical approaches to handling this:EAFP: Managing KeyError and exceptions
The EAFP pattern is considered more Pythonic and often performs better when keys usually exist, as it avoids redundant checks. It uses
try-exceptblocks to handle errors after they occur, assuming that operations will usually succeed. Let’s see how this works:However, there are scenarios where failing loudly, allowing the
KeyErrorto propagate, is actually preferable to silent failures. In the LBYL approach, you explicitly check for the key's existence before accessing it. Let’s see an example:Invoking this function with will raise
KeyErrorif any key in the dictionary is missing, which is correct behavior because it is better to fail during initialization than silently during production.Explicit error handling
When processing data from external sources like APIs or user input, explicit error handling becomes important. Let’s see how to do it with an example:
Understanding when to use
.get()versus bracket notation versustry-exceptis important.LBYL: Proactive membership testing
The LBYL approach uses conditional statements using the
inandnot inoperators to check for key existence before attempting access. This pattern is clearer when dealing with conditional logic or when you need to take different actions based on key presence. Let’s see an example of this:When validating dictionaries derived from external sources such as JSON from APIs, CSV files, or user input, proactive membership testing provides clear, readable validation logic. Let’s see this with an example:
Choosing the right approach
The choice between EAFP and LBYL often depends on your use case. Use EAFP when operations typically succeed and exceptions are rare. Use LBYL when you need explicit branching logic or when validating input before expensive operations.
Before iterating over a dictionary derived from an external source, it is best practice to:
By following these practices, you can make your code much more robust when dealing with unpredictable or incomplete data from APIs, user input, or configuration files.
For a more deeper look into writing resilient Python code, I recommend taking our course on Writing Efficient Python Code.
Advanced Python Dictionary Methods
As your data science projects grow in complexity, you’ll often need to combine, copy, and transform dictionaries. Learning these advanced operations enables you to manipulate data structures efficiently while avoiding subtle bugs that can derail pipelines. Let’s see some of these methods.
Merging operators
Python 3.9 introduced elegant union operators for merging dictionaries:
|(merge operator): Creates a new merged dictionary.|=(update operator): Updates an existing dictionary in place.These operators provide clean, readable syntax for combining data from multiple sources. Let’s see an example:
For Python versions before 3.9, the double-star unpacking method (
**) provides similar functionality:Collision precedence
Across all merging techniques, the rule is simple: last seen wins. The value from the right-hand dictionary overwrites the left-hand one if keys collide:
This behavior is consistent whether you use
|,|=, unpacking, or the.update()method. Order matters, making it especially useful for layered configuration management (e.g., system defaults < environment configs < user overrides).Copying mechanisms
One of the most dangerous pitfalls is misunderstanding how Python handles variable assignments.
Reference assignments create aliases, not copies
dict_a = dict_bdoes not create a copy. It creates a reference (an alias). Modifying one modifies the other. The following example illustrates the concept:As we can see, there is only one dictionary and both
originalandaliasreference it. Accordingly, when the value for the keyrecordsis set to2000, the change can only apply to this one dictionary.Shallow copies with .copy() or .dict()
A shallow copy (
.copy()ordict()) creates a new dictionary object, but nested mutable objects remain shared references.