HOWTO · Python

Array or List of Dictionaries in Python

Learn how to create, access, append to, and update a Python list of dictionaries.

On this page

A Python list is the usual container for a collection of dictionaries: each list element can hold a record of key-value pairs. Although people sometimes call this an array of dictionaries, a Python list and specialized array types are distinct. The examples below show how to create the list, access records and values, append a record, and update a value.

Create a List of Dictionaries in Python

Write each dictionary as an element inside a list literal. Each literal creates a separate dictionary, so records can use different keys or share some keys. Use a descriptive variable such as records; list is already the name of a Python built-in.

Run this example:

records = [
    {"name": "Ada", "role": "engineer"},
    {"name": "Lin", "role": "designer"},
]
print(records)
[{'name': 'Ada', 'role': 'engineer'}, {'name': 'Lin', 'role': 'designer'}]

Access a Dictionary and Its Values by Index and Key

Python list indexes start at 0, so index 1 selects the second dictionary. To read a field, first select the dictionary by its list index and then use the key in square brackets. The list and dictionary lookups are two steps in one expression.

records = [
    {"name": "Ada", "role": "engineer"},
    {"name": "Lin", "role": "designer"},
]
second_record = records[1]
role = records[1]["role"]
print(second_record)
print(role)
{'name': 'Lin', 'role': 'designer'}
designer

The key must exist in the selected dictionary for bracket lookup to succeed. When a key may be absent, use record.get("role") to return None by default, or provide a fallback such as record.get("role", "unknown").

Append a Dictionary to a List

Call append() to add one dictionary to the end of the existing list. It changes the list in place; it does not create a new list.

records = [{"name": "Ada", "role": "engineer"}]
records.append({"name": "Lin", "role": "designer"})
print(records)
[{'name': 'Ada', 'role': 'engineer'}, {'name': 'Lin', 'role': 'designer'}]

Update a Dictionary in the List

Select a record by index, then assign through a key to change a value. Assigning to a key that is not present adds that key, so this syntax can either replace a field’s value or add a field.

records = [{"name": "Ada", "role": "engineer"}]
records[0]["role"] = "lead engineer"
records[0]["team"] = "platform"
print(records)
[{'name': 'Ada', 'role': 'lead engineer', 'team': 'platform'}]

Create a List of Dictionaries with a List Comprehension

A list comprehension is concise when each record follows the same pattern. Put a dictionary literal in the expression to create a distinct dictionary on each iteration. For example, this creates four separate dictionaries initialized with the same fields:

records = [{"active": True, "score": 0} for _ in range(4)]
print(records)
[{'active': True, 'score': 0}, {'active': True, 'score': 0}, {'active': True, 'score': 0}, {'active': True, 'score': 0}]

To create several empty dictionaries instead, call dict() on each iteration:

records = [dict() for _ in range(3)]
print(records)
[{}, {}, {}]

A comprehension can also derive field values from an iterable:

records = [{"id": number, "label": f"item-{number}"} for number in range(3)]
print(records)
[{'id': 0, 'label': 'item-0'}, {'id': 1, 'label': 'item-1'}, {'id': 2, 'label': 'item-2'}]

Avoid multiplying a list containing one mutable dictionary to make repeated records. For example, [{}] * 3 repeats references to the same dictionary, so changing one element is visible through all three positions. A comprehension with {} creates a fresh dictionary for each iteration.

Create Dictionaries in a Loop

Use a loop when each dictionary needs several steps or conditional fields. Build a new dictionary inside each iteration, then append it to the list.

records = []
for number in range(3):
    record = {"id": number, "score": number * 10}
    records.append(record)

for record in records:
    print(record)
{'id': 0, 'score': 0}
{'id': 1, 'score': 10}
{'id': 2, 'score': 20}

Here, each pass through the loop creates one record using the current number; append() stores that dictionary in the list before the next pass.

Build Dictionaries with the dict() Constructor

The dict() constructor creates an empty dictionary with no arguments, or a dictionary from keyword fields. Keyword names become string keys:

blank_record = dict()
person = dict(name="John", age=30, city="New York")
print(blank_record)
print(person)
{}
{'name': 'John', 'age': 30, 'city': 'New York'}

You can use separate dict() calls to create dictionaries in a list:

records = [
    dict(name="Ada", role="engineer"),
    dict(name="Lin", role="designer"),
]
for record in records:
    print(record)
{'name': 'Ada', 'role': 'engineer'}
{'name': 'Lin', 'role': 'designer'}

Dictionary keys must be hashable, which includes strings, numbers, and tuples made from hashable values; mutable types such as lists cannot be keys. Each key appears at most once in a dictionary. If a dictionary is built from repeated key-value pairs, the later value replaces the earlier one. With keyword arguments to dict(), each keyword can appear only once in the call, so use a mapping or a sequence of pairs when the input may contain repeated keys.