Skip to content
Related Articles

Related Articles

How to create list of dictionary in Python

View Discussion
Improve Article
Save Article
  • Last Updated : 03 Feb, 2022
View Discussion
Improve Article
Save Article

In this article, we are going to discuss ways in which we can create a list of dictionaries in Python. 

A list of dictionaries means, the dictionary is present as an element in the list. 

Syntax:

[{‘key’:element1,’key’:element2,……, ‘key’:element n}]

Example: Python code to create a list of dictionary for students data

Python3




# create a list of dictionary with student
# id as key and name as value
data = [{7058: 'sravan', 7059: 'jyothika',
         7072: 'harsha', 7075: 'deepika'}]
 
# display data
data


Output:

[{7058: ‘sravan’, 7059: ‘jyothika’, 7072: ‘harsha’, 7075: ‘deepika’}]

We can access by using index

Syntax:

data[index][key]

where index is the dictionary index and the key is the dictionary key-value

Example: Python code to access keys via index

Python3




# create a list of dictionary with student
# id as key and name as value
data = [{7058: 'sravan', 7059: 'jyothika',
         7072: 'harsha', 7075: 'deepika'}]
 
# display data of key 7058
print(data[0][7058])
 
# display data of key 7059
print(data[0][7059])
 
# display data of key 7072
print(data[0][7072])
 
# display data of key 7075
print(data[0][7075])


Output:

sravan
jyothika
harsha
deepika

List of multiple dictionaries

This is similar to the above approach, except that multiple dictionaries are being passed to the list at once.

Example: Python program to create a list of multiple dictionaries 

Python3




# create a list of dictionaries with
# student id as key and name as value
data = [{7058: 'sravan', 7059: 'jyothika',
         7072: 'harsha', 7075: 'deepika'},
         
        {7051: 'fathima', 7089: 'mounika',
         7012: 'thanmai', 7115: 'vasavi'},
         
        {9001: 'ojaswi', 1289: 'daksha',
         7045: 'parvathi', 9815: 'bhavani'}]
 
print(data)


Output:

[{7058: ‘sravan’, 7059: ‘jyothika’, 7072: ‘harsha’, 7075: ‘deepika’}, {7051: ‘fathima’, 7089: ‘mounika’, 7012: ‘thanmai’, 7115: ‘vasavi’}, {9001: ‘ojaswi’, 1289: ‘daksha’, 7045: ‘parvathi’, 9815: ‘bhavani’}]

It can again access all the elements using index.

Example: Python code to access elements based on index and key

Python3




# create a list of dictionaries with
# student id as key and name as value
data = [{7058: 'sravan', 7059: 'jyothika',
         7072: 'harsha', 7075: 'deepika'},
         
        {7051: 'fathima', 7089: 'mounika',
         7012: 'thanmai', 7115: 'vasavi'},
         
        {9001: 'ojaswi', 1289: 'daksha',
         7045: 'parvathi', 9815: 'bhavani'}]
 
# access third dictionary with key 9001
print(data[2][9001])
 
# access second dictionary with key 7012
print(data[1][7012])
 
# access second dictionary with key 7115
print(data[1][7115])


Output:

ojaswi
thanmai
vasavi

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!