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: "GeeksforGeeks"} ]
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
data = [{7058: 'sravan', 7059: 'jyothika',
7072: 'harsha', 7075: 'deepika'}]
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
data = [{7058: 'sravan', 7059: 'jyothika',
7072: 'harsha', 7075: 'deepika'}]
print(data[0][7058])
print(data[0][7059])
print(data[0][7072])
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
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
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[2][9001])
print(data[1][7012])
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
data = [ {1: "Geeks", 2: "GeeksforGeeks"} ]
print(data)
new_data = {1: "Python", 2: "Programming"}
data.append(new_data)
print(data)
|
Output:
[{1: 'Geeks', 2: 'GeeksforGeeks'}]
[{1: 'Geeks', 2: 'GeeksforGeeks'}, {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
data = [{1: 'Geeks', 2: 'GeeksforGeeks'}, {1: 'Python', 2: 'Programming'}]
print(data)
data[0][3] = "World"
data[0][2] = "Hello"
del data[1][2]
print(data)
|
Output:
[{1: 'Geeks', 2: 'GeeksforGeeks'}, {1: 'Python', 2: 'Programming'}]
[{1: 'Geeks', 2: 'Hello', 3: 'World'}, {1: 'Python'}]
Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills, become a part of the hottest trend in the 21st century.Dive into the future of technology - explore the
Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
09 Aug, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...