List of Dictionaries in Python

Python List of dictionaries

We use both lists and dictionaries in our python programs. Lists are used to store atomic objects while dictionaries store key-value pairs. In this article, we will discuss how we can create a list of dictionaries in python. We will also discuss ways to search records in a list of dictionaries and also to sort them.

How to Create a List of Dictionaries in Python?

A list of dictionaries in python is a list having dictionaries as all of its elements. To create a list of dictionaries, we simply have to create an empty list and add dictionaries to it as the elements. Let us understand this using the following example.

Suppose that we have a list of names of laptops as follows.

Now we have to store the amount of RAM in each of the laptops with their name. For this, we cannot use a simple list. We will create a list of dictionaries to store the name and RAM of the laptops as discussed below.

For each laptop, we will create a dictionary with “name” and “RAM” as its keys with their associated values. After creating the dictionaries, we will create an empty list. Then, we will insert all the dictionaries into the list using the append() method as follows.

Output:

After all the append operations, we will get a list of dictionaries in the variable laptops. You can also observe this in the output.

Instead of using the append() method, you can also initialize the list with the created dictionaries to create a list of dictionaries. I would suggest you use this approach as this is more efficient in terms of code length as well as execution.

Output:

In the above example, the output is similar to the previous code. However, the above code has a much faster execution than the previous one.

How to Access Elements of a List of Dictionaries?

To access the elements from a list of dictionaries, we will first access each dictionary in the list using a for loop. After that, we will access each key-value pair in the dictionary using another for loop as shown below.

Output:

Search in a List of Dictionaries in Python

To search for an element in the list of dictionaries, we can use the for loop. While traversing the list of dictionaries, we can check for the key-value pairs for the desired value using the equality operator. Once we find the desired value, we can print all the associated information.

For instance, suppose that we have to search the name of the laptop that has the RAM value of 16. For this, we will traverse through each dictionary in the list of dictionaries and check if the value of RAM in the dictionary is 16. Once we find a dictionary with the RAM value 16, we will print the name of the laptop using the print() function as follows.

Output:

Update Values in a List of Dictionaries in Python

To update the values in a list of dictionaries in python, we will first search the dictionary to be updated using the approach discussed above. After that, we will update the value for the given key. For instance, we can update the RAM of laptop1 to 32 as follows.

Output:

In the output, we have printed the RAM of laptop1 before and after updating it. In the code, you can observe that we have to perform the search operation to select the dictionary to update. After finding the dictionary to be updated, we just update the desired key.

How to Sort List of Dictionaries in Python?

To sort a list of dictionaries in python, we will have to specify the key according to which the dictionaries will be sorted. We will choose a specific key-value pair from the dictionaries that will be used as the key for sorting the list of dictionaries. Here, we have to keep in mind that the key of the key-value pair that is to be used for comparing the dictionaries must be present in all the dictionaries in the list. 

For instance, if we have to sort the dictionaries of laptops according to their RAM value, we will first create a function getVal() that takes a dictionary as input and returns the value associated with the key “RAM” of the dictionary. After that, we will pass the getVal() function to the sort() method as an input argument for the parameter key. The sort() method when invoked on the list of dictionaries with the getVal() function as an input argument for the parameter “key”, will sort the list as follows.

Output:

Alternatively, you can use the sorted() function to get a new sorted list of dictionaries if you don’t want to change the existing list.

Instead of the getVal() function, we can also use the lambda functions to sort the list of dictionaries. Again, we will create a lambda function that takes a dictionary as input and returns the value associated with the key “RAM” of the dictionary. Then, we will pass this lambda function as an input argument to the “key” parameter of the sort() method to sort the list of dictionaries.

Output:

You can also use the sorted() function with the lambda function to sort the list of dictionaries if you don’t want to change the existing list.

Conclusion

In this article, we have discussed the list of dictionaries in python. We also saw how to create, update, search elements from, and sort a list of dictionaries using different approaches. 

I hope you enjoyed reading this article. Stay tuned for more informative articles.

Happy Learning!

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 26 December

    Repeat List N Times in Python

    Table of ContentsUsing the * operatorUsing the numpy.repeat() functionUsing the list comprehension techniqueUsing the itertools.repeat() functionConclusion This tutorial will demonstrate how to repeat list n times in Python. Using the * operator To repeat list n times in Python, use the * operator. Star operator(*) is used to multiply list by number e.g. lst*3 and […]

  • 16 December

    Convert List to Comma Separated String in Python

    Table of ContentsUse .join() MethodUse .join() with map() MethodUse .join() with List Comprehension Use .join() Method To convert a list to a comma separated string in Python, use the .join() method. join() method takes all elements in an iterable and joins them into one string with delimiter as separator. [crayon-63dd765e00a3a385364662/] [crayon-63dd765e00a41656123076/] First, we created a […]

  • 27 November

    Remove Punctuation from List in Python

    Table of ContentsUse for loop with replace() MethodUse Nested List ComprehensionUse str.translate() Function Use for loop with replace() Method To remove punctuation from list in Python: Use the for loop to iterate over each word (element) of given list. Use a nested loop to iterate over each character in the word. Use the if statement […]

  • 04 October

    Get Every Other Element in List in Python

    Table of ContentsGet Every Other Element in List in PythonUsing List SlicingUsing the list.append() function along with the range() functionUsing list comprehension and the range() function with conditionalsUsing list comprehension and the range() function with stepUsing the enumerate() function along with list comprehensionUsing a lambda functionConclusion. A list is one of the more versatile data […]

  • 23 September

    Add Tuple to List in Python

    Table of ContentsAdd tuple to list in PythonWays to Add Tuple to List in Python (adding the Tuple as An Element)Using the insert() Function to Add Tuple to List in PythonUsing the append() Function to Add Tuple to List in PythonWays to Add Tuple to List in Python (adding the Tuple’s Elements to A List)Using […]

  • 24 May

    Replace Comma with Space in List in Python

    Table of ContentsHow to replace comma with space in list in PythonUsing the replace() function to replace comma with space in list in PythonUsing the re.sub() function to replace comma with space in list in PythonUsing the split() and join() functions to replace comma with space in list in Python.Conclusion In this post, we will […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.