Javatpoint Logo
Javatpoint Logo

Python list of Dictionaries

In this tutorial, we will see how we can create a dictionary inside a list and then what are the operations that we can perform.

So let's start with creating a dictionary inside a list.

Consider the program given below,

Output:

The dictionary inside list is:  [{'English': 31101, 'Hindi': 31102, 'Mathematics': 31103, 'Physics': 31104, 'Chemistry': 31105}]
The type of list_val is:  <class 'list'>
The dictionary inside second list is:  [{'Apples': 'Red', 'Bananas': 'Yellow', 'Lemons': 'Yellow', 'Peas': 'Green', 'Strawberries': 'Pink'}]
The type of slist_val is:  <class 'list'>

Explanation-

Let's have a look at the explanation of the above program,

  1. In the first step, we have created a list and inside it, we have initialized the elements of the dictionary.
  2. We have then displayed the dictionary and then checked its type using type(), we can see that it comes out to be a list.
  3. After this, we have done the same thing with another list where the key-value pairs of the dictionary are in the form of strings.

Accessing the Values of Dictionary

Now, we shall see how we can access the values of a dictionary,

The program given below shows how it can done.

Code:

Output:

The dictionary inside list is:  [{'English': 31101}, {'Hindi': 31102}, {'Mathematics': 31103, 'Calculus': 311030}, {'Physics': 31104}, {'Chemistry': 31105}]
{'English': 31101}
31102
311030
The dictionary inside second list is:  [{'Apples': 'Red'}, {'Bananas': 'Yellow'}, {'Lemons': 'Yellow', 'Pineapple': 'Brown'}, {'Peas': 'Green'}, {'Strawberries': 'Pink'}]
{'Apples': 'Red'}
Yellow
Brown

Explanation-

Let's understand what we have done here,

  1. In the first step, we have created a list and inside it, we have initialized the elements of the dictionary.
  2. We have then accessed the values using indexing and further obtained the values by using the keys.
  3. After this, we have done the same thing with another list where the key-value pairs of the dictionary are in the form of strings.

Updating the Values of Dictionary

It's time to learn how the values of a dictionary can be updated,

The following program illustrates the same,

Output:

The dictionary inside list is:  [{'English': 31101}, {'Hindi': 31102}, {'Mathematics': 31103, 'Calculus': 311030}, {'Physics': 31104}, {'Chemistry': 31105}]
Updated dictionary is: [{'English': 31101}, {'Hindi': 41102}, {'Mathematics': 31103, 'Calculus': 311032}, {'Physics': 31104}, {'Chemistry': 31105}]
The dictionary inside second list is:  [{'Apples': 'Red'}, {'Bananas': 'Yellow'}, {'Lemons': 'Yellow', 'Pineapple': 'Brown'}, {'Peas': 'Green'}, {'Strawberries': 'Pink'}]
Updated dictionary is: [{'Apples': 'Red'}, {'Bananas': 'Green'}, {'Lemons': 'Yellow', 'Pineapple': 'Peach'}, {'Peas': 'Green'}, {'Strawberries': 'Pink'}]

Explanation-

Let's check out what exactly happened in the above program,

  1. In the first step, we have created a list and inside it, we have initialized the elements of the dictionary.
  2. We have then updated the values using indexing and then stored the new values with the help of the assignment operator.
  3. After this, we have done the same thing with another list where the key-value pairs of the dictionary are in the form of strings.

Appending the Values of the Dictionary

Finally, we will learn how we can append the values in a dictionary.

Consider the program given below,

Output:

The dictionary inside list is:  [{'English': 31101}, {'Hindi': 31102}, {'Mathematics': 31103, 'Calculus': 311030}, {'Physics': 31104}, {'Chemistry': 31105}]
List after the appended values is:  [{'English': 31101}, {'Hindi': 31102}, {'Mathematics': 31103, 'Calculus': 311030}, {'Physics': 31104}, {'Chemistry': 31105}, {'Biology': 31106, 'Physical Training': 31107}]

Explanation-

It's time to have a glance at the explanation of this program,

  1. In the first step, we have created a list and inside it, we have initialized the elements of the dictionary.
  2. We have then appended the values using the append() method and specified the elements to be added to it.

Conclusion

In this tutorial, we learned how we can create a list of dictionaries and access, update and append the values in it.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA