How to create list of dictionary in Python
Last Updated :
25 Nov, 2024
In this article, we are going to discuss ways in which we can create a list of dictionaries in Python. Let’s start with a basic method to create a list of dictionaries using Python.
Python
# Creating a list of dictionaries
a = [{"name": "Alice", "age": 25}, {"name": "Bob", "age": 30}]
print(a)
print(type(a))
Output[{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}]
<class 'list'>
We created a list a containing two dictionaries. Each dictionary has keys like "name" and "age" and their corresponding values. Let's explore other methods to create a list of dictionaries.
Creating a List of Dictionaries Using a Loop
Creating a list of dictionaries using a for loop is efficient approach. We can dynamically generate dictionaries.
Python
a = []
# Using a loop to create a list of dictionaries
for i in range(3):
a.append({"name": f"Person {i+1}", "age": 20 + i})
print(a)
Output[{'name': 'Person 1', 'age': 20}, {'name': 'Person 2', 'age': 21}, {'name': 'Person 3', 'age': 22}]
Creating a List of Dictionaries using List Comprehension
List comprehension is a compact way to create lists. We can use it to create a list of dictionaries in just one line.
Python
a = [{"name": f"Person {i+1}", "age": 20 + i} for i in range(3)]
# Using list comprehension to create a list of dictionaries
print(a)
Output[{'name': 'Person 1', 'age': 20}, {'name': 'Person 2', 'age': 21}, {'name': 'Person 3', 'age': 22}]
Creating a List of Dictionaries from Lists
If we already have a list of data, and we want to turn it into a list of dictionaries. We can use a loop or list comprehension to do this.
Python
# Creating a list of dictionaries from another list
names = ["Alice", "Bob", "Charlie"]
ages = [25, 30, 22]
a = [{"name": names[i], "age": ages[i]} for i in range(len(names))]
print(a)
Output[{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 22}]
Accessing Dictionary Elements from a List of Dictionaries
We can access the elements in a list of dictionaries by using indexing for the dictionary and the keys for specific values inside the dictionary.
Python
lod = [
{'name': 'Alice', 'age': 25, 'city': 'New York'},
{'name': 'Bob', 'age': 30, 'city': 'Los Angeles'},
{'name': 'Charlie', 'age': 35, 'city': 'Chicago'}
]
# Access the second dictionary in the list
d1 = lod[1]
print(d1)
print(lod[0]['name'])
Output{'name': 'Bob', 'age': 30, 'city': 'Los Angeles'}
Alice
Similar Reads
How to Create a Dictionary in Python The task of creating a dictionary in Python involves storing key-value pairs in a structured and efficient manner, enabling quick lookups and modifications. A dictionary is an unordered, mutable data structure where each key must be unique and immutable, while values can be of any data type. For exa
3 min read
Ways to create a dictionary of Lists - Python A dictionary of lists is a type of dictionary where each value is a list. These dictionaries are commonly used when we need to associate multiple values with a single key.Initialize a Dictionary of ListsThis method involves manually defining a dictionary where each key is explicitly assigned a list
3 min read
Convert a list of Tuples into Dictionary - Python Converting a list of tuples into a dictionary involves transforming each tuple, where the first element serves as the key and the second as the corresponding value. For example, given a list of tuples a = [("a", 1), ("b", 2), ("c", 3)], we need to convert it into a dictionary. Since each key-value p
3 min read
How to Add Duplicate Keys in Dictionary - Python In Python, dictionaries are used to store key-value pairs. However, dictionaries do not support duplicate keys. In this article, we will explore several techniques to store multiple values for a single dictionary key.Understanding Dictionary Key ConstraintsIn Python, dictionary keys must be unique.
3 min read
Iterate over a dictionary in Python In this article, we will cover How to Iterate Through a Dictionary in Python. To Loop through values in a dictionary you can use built-in methods like values(), items() or even directly iterate over the dictionary to access values with keys.How to Loop Through a Dictionary in PythonThere are multipl
6 min read