How to create a List of Dictionaries in Python: Create and Traverse

The list is a data structure where you can store multiple objects in a single variable. And the dictionary is an object that has key-value pair as a variable. In this tutorial, you will learn how to create a list of dictionaries in python with steps.

Syntax of the List and dictionary in python

The syntax of the dictionary is below.

{"your_key":"your_value"}

And the syntax of the list is below.

[your_list]

Creating a List Of Dictionaries In Python

You can create a list of dictionaries by separating each dictionary key value by a comma in a square bracket.  Suppose you have more than one dictionary assigned to each variable. Then the following lines of code will create a list of dictionaries.

d1 = {"country":"USA","capital":"Washington DC"}
d2 = {"country":"UK","capital":"London"}
d3 = {"country":"India","capital":"New Delhi"}
list_of_dict = [d1,d2,d3]
print(list_of_dict)

Output

[{'country': 'USA', 'capital': 'Washington DC'}, {'country': 'UK', 'capital': 'London'},
 {'country': 'India', 'capital': 'New Delhi'}]

You can also add all the dictionaries at once inside the square bracket each separated by a comma.

sample_dict = [{'country': 'USA', 'capital': 'Washington DC'},
               {'country': 'UK', 'capital': 'London'},
               {'country': 'India', 'capital': 'New Delhi'}]
print(sample_dict)

Output

[{'country': 'USA', 'capital': 'Washington DC'}, {'country': 'UK', 'capital': 'London'}, 
{'country': 'India', 'capital': 'New Delhi'}]

Accessing List Of Dictionaries In Python

Traversing and accessing the list of dictionaries is very simple. Below are the steps that can help you traverse or access the list of dictionaries.

Step 1: First go to the key-value pair

Before getting the data of the value from the key of a particular dictionary you have to first traverse to that key. You will use the index for that. For example, I want to go to country UK and find its capital. To do so you have to first reverse to that country using the index.

d1 = {"country":"USA","capital":"Washington DC"}
d2 = {"country":"UK","capital":"London"}
d3 = {"country":"India","capital":"New Delhi"}
list_of_dict = [d1,d2,d3]
list_of_dict[1]

Output

{'country': 'UK', 'capital': 'London'}

Step 2: Get the value

The second and last step is to get the value using the key. To get the capital of the UK you have to get use the string “capital” inside the square bracket.

Run the below code to get the capital of the country UK.

d1 = {"country":"USA","capital":"Washington DC"}
d2 = {"country":"UK","capital":"London"}
d3 = {"country":"India","capital":"New Delhi"}
list_of_dict = [d1,d2,d3]
list_of_dict[1]["capital"]

Output

London

Conclusion

You can use the square bracket [] to create a list of dictionaries. In this entire post, you have learned how to create a list of dictionaries and steps to access and traverse along it. If you have any questions on this topic then you can contact us for getting more help.

Hi, I am CodeTheBest. Here you will learn the best coding tutorials on the latest technologies like a flutter, react js, python, Julia, and many more in a single place.

SPECIAL OFFER!

This Offer is Limited! Grab your Discount!
15000 ChatGPT Prompts
Offer Expires In:
close-link