Python dictionaries are one of the most versatile and frequently used data structures. Understanding how to effectively use them is important for any Python programmer. This post provides an overview of dictionaries, covering their creation, manipulation, and common use cases, with plenty of code examples to illustrate each concept.
What are Python Dictionaries?
Dictionaries in Python are unordered collections of key-value pairs. Each key is unique and immutable (typically a string or number), while the associated value can be of any data type. This key-value structure allows for efficient lookups and retrieval of data based on the key. Think of them as real-world dictionaries where you look up a word (key) to find its definition (value).
Creating Dictionaries
There are many ways to create dictionaries in Python:
1. Using curly braces {}
:
This is the most common method. Key-value pairs are separated by colons, and pairs are separated by commas.
= {"name": "Alice", "age": 30, "city": "New York"}
my_dict print(my_dict) # Output: {'name': 'Alice', 'age': 30, 'city': 'New York'}
2. Using the dict()
constructor:
You can also create dictionaries using the dict()
constructor.
= dict(name="Bob", age=25, city="London")
my_dict print(my_dict) # Output: {'name': 'Bob', 'age': 25, 'city': 'London'}
3. From a list of tuples:
If you have a list of tuples where each tuple represents a key-value pair, you can use the dict()
constructor to create a dictionary.
= [("name", "Charlie"), ("age", 35), ("city", "Paris")]
my_list = dict(my_list)
my_dict print(my_dict) # Output: {'name': 'Charlie', 'age': 35, 'city': 'Paris'}
Accessing Dictionary Values
You can access the value associated with a key using square bracket notation:
= {"name": "Alice", "age": 30, "city": "New York"}
my_dict print(my_dict["name"]) # Output: Alice
Trying to access a key that doesn’t exist will raise a KeyError
. To avoid this, you can use the get()
method, which returns a default value (None by default) if the key is not found.
print(my_dict.get("country")) # Output: None
print(my_dict.get("country", "Unknown")) # Output: Unknown
Modifying Dictionaries
Adding, updating, and deleting key-value pairs is straightforward:
Adding a new key-value pair:
"occupation"] = "Engineer"
my_dict[print(my_dict) # Output: {'name': 'Alice', 'age': 30, 'city': 'New York', 'occupation': 'Engineer'}
Updating an existing key-value pair:
"age"] = 31
my_dict[print(my_dict) # Output: {'name': 'Alice', 'age': 31, 'city': 'New York', 'occupation': 'Engineer'}
Deleting a key-value pair:
del my_dict["city"]
print(my_dict) # Output: {'name': 'Alice', 'age': 31, 'occupation': 'Engineer'}
The pop()
method removes a key and returns its value. It also takes an optional second argument specifying a default value to return if the key is not found.
= my_dict.pop("age")
age print(age) # Output: 31
print(my_dict) # Output: {'name': 'Alice', 'occupation': 'Engineer'}
= my_dict.pop("country", "Not specified")
country print(country) # Output: Not specified
Iterating Through Dictionaries
You can iterate through the keys, values, or key-value pairs of a dictionary using loops:
Iterating through keys:
for key in my_dict:
print(key)
Iterating through values:
for value in my_dict.values():
print(value)
Iterating through key-value pairs:
for key, value in my_dict.items():
print(f"Key: {key}, Value: {value}")
Common Dictionary Methods
Python offers many built-in methods for working with dictionaries, including clear()
, copy()
, keys()
, values()
, items()
, popitem()
, and more. Refer to the official Python documentation for a complete list.
Dictionary Comprehensions
Similar to list comprehensions, dictionary comprehensions provide a concise way to create dictionaries.
= {x: x*x for x in range(1, 6)}
squares print(squares) # Output: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
This creates a dictionary where keys are numbers from 1 to 5 and values are their squares. This is a powerful technique for creating dictionaries in a compact and readable manner.