Python list of DictionariesIn 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,
Accessing the Values of DictionaryNow, 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,
Updating the Values of DictionaryIt'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,
Appending the Values of the DictionaryFinally, 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,
ConclusionIn this tutorial, we learned how we can create a list of dictionaries and access, update and append the values in it. Next TopicPython Shelve Module |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India