Friday, December 27, 2024
Google search engine
HomeLanguagesHow to create list of dictionary in Python

How to create list of dictionary in Python

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

Input: d = [{7058: 'sravan', 7059: 'jyothika', 7072: 'harsha', 7075: 'deepika'}] 
print(type(d))
Output: <class 'list'>
Explanation: The type of input is list data-type having dictionary init.

List of Dictionary in Python

Below are the topics that we will cover in this article:

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

Example:

[ {1: "Geeks", 2: "Lazyroar"} ]

Create a List of Dictionary in Python

A Python list of dictionaries can be created with the following syntax:

Syntax:

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

Example: In this example, We will create a list and pass a dictionary to it. In this case, we pass only a single dictionary, so the length of the list will be 1.

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
print(data)
print(len(data))
print(type(data))


Output:

[{7058: 'sravan', 7059: 'jyothika', 7072: 'harsha', 7075: 'deepika'}]
1
<class 'list'>

Accessing Dictionary Elements from a Python List of Dictionary

We can access the elements of a Python dictionary by using the index, where the index is the dictionary index and the key is the dictionary key-value

Syntax:

data[index][key]

Example: Here we will get the values of the dictionary via index value.

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

Create a List of Multiple Dictionary in Python

This is similar to the above approach, except that multiple dictionaries are being passed to the list at once. A list of dictionaries can be created in Python with the following

Syntax:

[ {“key1”: “element1”, “key2”: “element2”},

{“key1: “element1”, “key2”: “element2”} ]

Example: In this program, we will create a list in Python of length 3 where the elements of the list will be 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'}]

Accessing Dictionary Elements from a Python List of Multiple Dictionaries

Similar to the list of single Python dictionaries, we can again access all the elements using an index.

Example: In this example, we will access particular elements based on an index and key values of the dictionary in Python.

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

Append a Dictionary to Python List of Dictionary

We can append a new dictionary to the list of dictionaries by using the Python append() method.

Example: In this example, we have a list of a single dictionary element. We will add another dictionary to this list using the append() method.

Python3




# create a list of a dictionary
# with single dictionary element
data = [ {1: "Geeks", 2: "Lazyroar"} ]
print(data)
 
# create a new dictionary to be appended
new_data = {1: "Python", 2: "Programming"}
 
# appending the new dictionary to
# the original list of dictionary
data.append(new_data)
print(data)


Output:

[{1: 'Geeks', 2: 'Lazyroar'}]
[{1: 'Geeks', 2: 'Lazyroar'}, {1: 'Python', 2: 'Programming'}]

Update a Dictionary to Python List of Dictionary

We can also update a value of a Python dictionary in the list of dictionaries.

Example: In this example, we will update the existing list of dictionaries in various ways. First, we update a dictionary of the list by adding a new value to it. Second, we update an existing value of the dictionary and third, we delete a dictionary key-value element using the del keyword.

Python3




# create a list of a dictionaries
data = [{1: 'Geeks', 2: 'Lazyroar'}, {1: 'Python', 2: 'Programming'}]
print(data)
 
# update the dictionary value
data[0][3] = "World"
data[0][2] = "Hello"
del data[1][2]
 
print(data)


Output:

[{1: 'Geeks', 2: 'Lazyroar'}, {1: 'Python', 2: 'Programming'}]
[{1: 'Geeks', 2: 'Hello', 3: 'World'}, {1: 'Python'}]

RELATED ARTICLES

Most Popular

Recent Comments