In Python, working with complex data structures is common, and one of the most powerful combinations is a list of dictionaries. Whether you are storing records, managing data entries, or organizing structured information, a Python list of dictionaries offers flexibility and efficiency. In this guide, you learn how to create, access, modify, and sort a list of dict Python style, with practical examples.
Let’s dive deep into list and dictionary in Python concepts and see how you can master them!

Table of Contents
What Is a Python List of Dictionaries?
A Python list of dictionaries is simply a list where each element is a dictionary. Each dictionary holds key-value pairs, allowing for structured data storage inside a list.
Here’s a simple example:
people = [
{"name": "Alice", "age": 30},
{"name": "Bob", "age": 25},
{"name": "Charlie", "age": 35}
]In this case, people is a dict list Python structure: a list containing multiple dictionaries.
How to Create a List of Dictionaries in Python
Creating a list of dict Python objects is straightforward. You can define each dictionary individually and add them to a list:
person1 = {"name": "Alice", "age": 30}
person2 = {"name": "Bob", "age": 25}
people = [person1, person2]Or create them directly inside the list:
people = [
{"name": "Alice", "age": 30},
{"name": "Bob", "age": 25}
]Knowing how to create Python dictionary structures effectively is crucial for handling dynamic data.
Accessing Items in a Python List of Dictionaries
You can access specific dictionaries by their list index, and then access values by dictionary keys.
Example:
print(people[0]["name"]) # Outputs: Alice
print(people[1]["age"]) # Outputs: 25You can also loop through the list to access each dictionary’s dict items in Python:
for person in people:
print(person["name"], person["age"])Understanding how to work with both list and dictionary in Python allows for clean, readable code when dealing with collections of structured data.
How to Update Values in a Python List of Dictionaries
Modifying dictionary values inside a list is easy. Simply access the dictionary through its index and update the key:
people[0]["age"] = 31
print(people[0]) # {'name': 'Alice', 'age': 31}You can also add new keys:
people[1]["city"] = "New York"
print(people[1]) # {'name': 'Bob', 'age': 25, 'city': 'New York'}Sorting a Python List of Dictionaries by a Specific Value
One common task is to sort a dictionary by value in Python, meaning, you sort the list based on a value inside the dictionaries.
For example, to sort people by the age key:
sorted_people = sorted(people, key=lambda x: x["age"])
print(sorted_people)This will order the list from the youngest to the oldest person.
Sorting is a powerful tool, especially when working with large datasets using a dict list Python approach.
Working with Python Dictionary Values
When dealing with a list of dictionaries in Python, you often need to access just the dictionary values.
Example: printing all names in the list:
for person in people:
print(person.get("name"))Using .get() is safer than direct key access because it avoids errors if the key is missing.
Working with Python dictionary value operations ensures your code is both efficient and error-resistant.
FAQ About Python List of Dictionaries
How do I append a new dictionary to an existing list?
You can use the append() method:
new_person = {"name": "Daisy", "age": 28}
people.append(new_person)How do I delete a dictionary from a list?
You can remove it by index:
del people[1] # Deletes Bob's dictionaryOr by matching a condition:
people = [person for person in people if person["name"] != "Bob"]Can I have nested dictionaries inside a list?
Yes! Dictionaries inside the list can have other dictionaries as values:
people = [
{"name": "Alice", "address": {"city": "Boston", "zip": "02101"}},
{"name": "Bob", "address": {"city": "Chicago", "zip": "60601"}}
]You would access nested values with:
print(people[0]["address"]["city"]) # BostonPython List of Dictionaries Conclusion
Mastering the Python list of dictionaries is an essential skill for any programmer working with structured data. Whether you’re looking to create, access, modify, or sort your data, understanding how to combine a list and dictionary in Python will greatly enhance your coding ability.
Make sure to practice these examples and experiment with your own data structures. If you want to explore more about dictionaries, lists, and how they work together, check out the rest of our Python Programming tutorials.
Happy coding!