Last modified: Aug 17, 2026
Python Array of Dictionaries: Complete Guide
Python is a versatile language. It offers many ways to organize data. One powerful structure is an array of dictionaries. This guide explains it clearly. You will learn how to create, access, and modify them easily.
An array of dictionaries is simply a list. Each item in this list is a dictionary. This structure is perfect for storing related data. Think of a list of users or products. Each dictionary holds the details. This guide is perfect for beginners. You will gain practical skills quickly.
What is an Array of Dictionaries?
In Python, an array is often a list. A dictionary stores key-value pairs. Combining them gives you a list of dictionaries. This is very useful for structured data.
Each dictionary can have different keys. But usually, they share the same structure. This makes data easy to manage. For example, you can store employee records. Each record has a name, age, and department. This is much cleaner than separate lists.
You can also think of it as a table. The list is the table. Each dictionary is a row. The keys are the column headers. This mental model is very helpful. It makes complex data simple to understand and process.
Creating an Array of Dictionaries
Creating this structure is straightforward. You start with a list. Then, you add dictionaries inside it. Let's look at a basic example.
# Creating an array of dictionaries
users = [
{"name": "Alice", "age": 25, "city": "New York"},
{"name": "Bob", "age": 30, "city": "London"},
{"name": "Charlie", "age": 35, "city": "Paris"}
]
print(users)
Here, users is our list. It contains three dictionaries. Each dictionary has the same keys. This consistency is good. It makes data processing predictable.
[{'name': 'Alice', 'age': 25, 'city': 'New York'}, {'name': 'Bob', 'age': 30, 'city': 'London'}, {'name': 'Charlie', 'age': 35, 'city': 'Paris'}]
You can also create an empty list. Then, you can add dictionaries later. This is useful for building data dynamically. Use the append() method to add new items.
# Creating an empty list and adding dictionaries
products = []
products.append({"id": 1, "name": "Laptop", "price": 1000})
products.append({"id": 2, "name": "Mouse", "price": 25})
print(products)
This method is powerful. It allows you to grow your data set easily. You can add data from user input or files. This is a common pattern in real-world applications.
Accessing Data in the Array
Accessing data requires two steps. First, you access the dictionary by its index. Then, you access the value by its key. This is very intuitive.
# Accessing data from the array
users = [
{"name": "Alice", "age": 25, "city": "New York"},
{"name": "Bob", "age": 30, "city": "London"}
]
# Get the first user's name
first_user_name = users[0]["name"]
print(first_user_name)
# Get the second user's city
second_user_city = users[1]["city"]
print(second_user_city)
The index is the position in the list. Python uses zero-based indexing. So, users[0] is the first dictionary. Then, ["name"] gets the value for that key.
Alice
London
You can also use negative indexing. users[-1] gets the last dictionary. This is a handy shortcut. It helps you access data from the end quickly.
Looping Through the Array
Looping is essential for processing all data. You can use a for loop. This lets you access each dictionary one by one. It is very efficient.
# Looping through the array of dictionaries
users = [
{"name": "Alice", "age": 25},
{"name": "Bob", "age": 30},
{"name": "Charlie", "age": 35}
]
for user in users:
print(user["name"], "is", user["age"], "years old.")
In each iteration, user is a dictionary. You can access its keys directly. This makes it easy to perform actions on each record.
Alice is 25 years old.
Bob is 30 years old.
Charlie is 35 years old.
You can also use enumerate() if you need the index. This gives you both the position and the dictionary. It is useful for tracking your progress.
# Using enumerate to get index and data
for index, user in enumerate(users):
print(f"Index {index}: {user['name']}")
This approach is very common. It combines the power of loops with dictionary access. It is a core skill for any Python developer.
Modifying Dictionaries in the Array
You can change data in your array. You can update existing values. You can also add new key-value pairs. This makes your data dynamic.
# Modifying data in the array
users = [
{"name": "Alice", "age": 25},
{"name": "Bob", "age": 30}
]
# Update Alice's age
users[0]["age"] = 26
print(users[0])
# Add a new key to Bob's dictionary
users[1]["city"] = "London"
print(users[1])
Updating is simple. You assign a new value to an existing key. Adding is just as easy. You assign a value to a new key. Python handles this automatically.
{'name': 'Alice', 'age': 26}
{'name': 'Bob', 'age': 30, 'city': 'London'}
This flexibility is a key advantage. It allows you to keep your data up to date. You can easily fix errors or add new information. This is essential for many applications.
Adding and Removing Dictionaries
Managing the array itself is also important. You can add new dictionaries. You can also remove old ones. This keeps your data set relevant.
# Adding and removing dictionaries
users = [
{"name": "Alice", "age": 25},
{"name": "Bob", "age": 30}
]
# Add a new user
users.append({"name": "Charlie", "age": 35})
print(users)
# Remove the last user
users.pop()
print(users)
# Remove by index
users.pop(0)
print(users)
The append() method adds to the end. The pop() method removes. Without an argument, it removes the last item. With an index, it removes that specific item.
[{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 35}]
[{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}]
[{'name': 'Bob', 'age': 30}]
These methods are crucial for data management. They allow you to control the size of your array. This is a fundamental part of working with collections.
Practical Example: Filtering Data
Let's apply what you learned. We will filter data based on a condition. This is a common task in programming.
# Filtering users by age
users = [
{"name": "Alice", "age": 25},
{"name": "Bob", "age": 30},
{"name": "Charlie", "age": 35},
{"name": "David", "age": 20}
]
# Get users older than 25
adults = [user for user in users if user["age"] > 25]
print(adults)
This uses a list comprehension. It creates a new list. It only includes dictionaries that meet the condition. This is a clean and efficient way to filter.
[{'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 35}]
This pattern is extremely powerful. You can filter based on any key. You can also sort or transform the data. This is the foundation of data analysis in Python.
Sorting the Array
Sorting is another important operation. You can sort by any key in the dictionaries. Python provides a simple way to do this.
# Sorting users by age
users = [
{"name": "Charlie", "age": 35},
{"name": "Alice", "age": 25},
{"name": "Bob", "age": 30}
]
# Sort by age in ascending order
users.sort(key=lambda x: x["age"])
print(users)
# Sort by name
users.sort(key=lambda x: x["name"])
print(users)
The sort() method uses a key function. This function tells Python what to sort by. Here, we sort by the "age" key and then the "name" key.
[{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 35}]
[{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 35}]
Sorting is essential for presenting data. It makes information easier to read and analyze. This is a skill you will use often.
Working with Nested Data
Sometimes, your data can be more complex. A dictionary might contain another list. This is called nesting. It is very common in real-world scenarios.
# Example with nested lists in dictionaries
students = [
{"name": "Alice", "grades": [85, 90, 92]},
{"name": "Bob", "grades": [78, 82, 88]}
]
# Access Alice's second grade
alice_grade = students[0]["grades"][1]
print(alice_grade)
# Calculate Bob's average grade
bob_average = sum(students[1]["grades"]) / len(students[1]["grades"])
print(bob_average)
You can access nested data by chaining indices and keys. This allows you to work with very complex structures. It is a powerful feature of Python.
90
82.66666666666667
Understanding nesting is crucial. It opens up a world of possibilities. You can model almost any real-world data structure.
Conclusion
Arrays of dictionaries are a powerful tool. They let you store structured data easily. You can create, access, and modify them with simple syntax. This guide covered the essentials.
You learned how to create an array. You learned how to loop through it. You also learned how to filter and sort. These are core skills for data manipulation.
Practice these examples yourself. Try creating your own data sets. Experiment with different keys and values. The more you practice, the more comfortable you will become. This is a fundamental concept that will help you in many projects.
For more advanced array operations, check out our guide on merging and sorting arrays. You might also find our guide on splitting arrays into chunks helpful. And if you're dealing with string data, our guide on arrays of strings is a great resource.
Remember, the key to mastering Python is practice. Use these structures in your own projects. You will soon find them indispensable. Happy coding!