Skip to content
Related Articles

Related Articles

Improve Article

How to create list of dictionary in Python

  • Last Updated : 28 Jul, 2021

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. 

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

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 wih key 9001
print(data[2][9001])
  
# access second dictionary wih key 7012
print(data[1][7012])
  
# access second dictionary wih key 7115
print(data[1][7115])

Output:

ojaswi
thanmai
vasavi



My Personal Notes arrow_drop_up
Recommended Articles
Page :