Python List of Dictionaries

Today, We want to share with you list of dictionaries python.In this post we will show you python array of dictionaries, hear for creating a list of dictionaries in python we will give you demo and example for implement.In this post, we will learn about python if not with an example.

List of Dictionaries in Python

In Python Program, you can have a List of Dictionaries. You already know that elements of the Python Program List could be objects of any type.

In this tutorial, I will learn how to make a list of dictionaries, how to get them, how to append a dictionary to list as well as how to edit them.

Create a List of Dictionaries in Python Program

In the below program, I make a list of length three, where all the 3 main elements are of type dict.

Read Also:  How to Call Function on Tkinter Button Click event in python?

Example

allRounder = [
	{
		'pakainfo':12,
		'tamilrocker':14
	},
	{
		'mp3goo':52,
		'gandhi':641
	},
	{
		'modi':6,
		'loveu':84
	}
]

print(allRounder)

Output

[{'pakainfo': 12, 'tamilrocker': 14}, {'mp3goo': 52, 'gandhi': 641}, {'modi': 6, 'loveu': 84}]

Each element of the list is a dictionary.

Access data key:value pairs in List of Dictionaries

Dictionary is like any element in a list. Therefore, you can access each dictionary of the list using index.

And I know how to access a specific data key:value of the dictionary using key.

In the below program, I shall print some of the values of dictionaries in list using keys.

allRounder = [
	{
		'pakainfo':12,
		'tamilrocker':14
	},
	{
		'mp3goo':52,
		'gandhi':641
	},
	{
		'modi':6,
		'loveu':84
	}
]

print(allRounder[0])
print(allRounder[0]['tamilrocker'])

print(allRounder[1])
print(allRounder[1]['mp3goo'])

print(allRounder[2])
print(allRounder[2]['modi'])

Output

{'pakainfo': 12, 'tamilrocker': 14}
14
{'mp3goo': 52, 'gandhi': 641}
52
{'modi': 6, 'loveu': 84}
6

Update key:value pairs of a Dictionary in List of Dictionaries

In the below program, I shall update some of the key:value pairs of dictionaries in list: Update data value for a key in the first dictionary, add a key:value pair to the second dictionary, delete a key:value pair from the third dictionary.
Example

allRounder = [
	{
		'pakainfo':12,
		'tamilrocker':14
	},
	{
		'mp3goo':52,
		'gandhi':641
	},
	{
		'modi':6,
		'loveu':84
	}
]

#update value for 'tamilrocker' in first dictionary
allRounder[0]['tamilrocker'] = 52

#add a new key:value pair to second dictionary
allRounder[1]['gar'] = 38

#delete a key:value pair from third dictionary
del allRounder[2]['modi']

print(allRounder)

Output

[{'pakainfo': 12, 'tamilrocker': 52}, {'mp3goo': 52, 'gandhi': 641, 'gar': 38}, {'loveu': 84}]

Append a Dictionary to List of Dictionaries

In the below program, I shall append a dictionary to the list of dictionaries.
Example

allRounder = [
	{
		'pakainfo':12,
		'tamilrocker':14
	},
	{
		'mp3goo':52,
		'gandhi':641
	},
	{
		'modi':6,
		'loveu':84
	}
]

#append dictionary to list
allRounder.append({'sejal':48, 'infinityknow':28})

print(allRounder)

Output

[{'pakainfo': 12, 'tamilrocker': 14}, {'mp3goo': 52, 'gandhi': 641}, {'modi': 6, 'loveu': 84}, {'sejal': 48, 'infinityknow': 28}]

list of dictionaries in Python Program as well as different operations on the elements of it


I hope you get an idea about list of dictionary python.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.