Table of Contents
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.
1 2 3 |
laptops = ["laptop1", "laptop12", "laptop22", "laptop124", "laptop34"] |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
myLaptop1 = {"name": "laptop1", "RAM": 16} myLaptop2 = {"name": "laptop12", "RAM": 32} myLaptop3 = {"name": "laptop22", "RAM": 64} myLaptop4 = {"name": "laptop124", "RAM": 4} myLaptop5 = {"name": "laptop34", "RAM": 8} laptops = list() laptops.append(myLaptop1) laptops.append(myLaptop2) laptops.append(myLaptop3) laptops.append(myLaptop4) laptops.append(myLaptop5) print("Elements in the list are:") for element in laptops: print(element) |
Output:
1 2 3 4 5 6 7 8 |
Elements in the list are: {'name': 'laptop1', 'RAM': 16} {'name': 'laptop12', 'RAM': 32} {'name': 'laptop22', 'RAM': 64} {'name': 'laptop124', 'RAM': 4} {'name': 'laptop34', 'RAM': 8} |
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.
1 2 3 4 5 6 7 8 9 10 11 |
myLaptop1 = {"name": "laptop1", "RAM": 16} myLaptop2 = {"name": "laptop12", "RAM": 32} myLaptop3 = {"name": "laptop22", "RAM": 64} myLaptop4 = {"name": "laptop124", "RAM": 4} myLaptop5 = {"name": "laptop34", "RAM": 8} laptops = [myLaptop1, myLaptop2, myLaptop3, myLaptop4, myLaptop5] print("Elements in the list are:") for element in laptops: print(element) |
Output:
1 2 3 4 5 6 7 8 |
Elements in the list are: {'name': 'laptop1', 'RAM': 16} {'name': 'laptop12', 'RAM': 32} {'name': 'laptop22', 'RAM': 64} {'name': 'laptop124', 'RAM': 4} {'name': 'laptop34', 'RAM': 8} |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
myLaptop1 = {"name": "laptop1", "RAM": 16} myLaptop2 = {"name": "laptop12", "RAM": 32} myLaptop3 = {"name": "laptop22", "RAM": 64} myLaptop4 = {"name": "laptop124", "RAM": 4} myLaptop5 = {"name": "laptop34", "RAM": 8} laptops = list() laptops.append(myLaptop1) laptops.append(myLaptop2) laptops.append(myLaptop3) laptops.append(myLaptop4) laptops.append(myLaptop5) for laptop in laptops: for key in laptop: print("{} is {}".format(key, laptop[key])) |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 |
name is laptop1 RAM is 16 name is laptop12 RAM is 32 name is laptop22 RAM is 64 name is laptop124 RAM is 4 name is laptop34 RAM is 8 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
myLaptop1 = {"name": "laptop1", "RAM": 16} myLaptop2 = {"name": "laptop12", "RAM": 32} myLaptop3 = {"name": "laptop22", "RAM": 64} myLaptop4 = {"name": "laptop124", "RAM": 4} myLaptop5 = {"name": "laptop34", "RAM": 8} laptops = [myLaptop1, myLaptop2, myLaptop3, myLaptop4, myLaptop5] for laptop in laptops: if laptop["RAM"] == 16: print("{} has RAM of 16 GB".format(laptop["name"])) |
Output:
1 2 3 |
laptop1 has RAM of 16 GB |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
myLaptop1 = {"name": "laptop1", "RAM": 16} myLaptop2 = {"name": "laptop12", "RAM": 32} myLaptop3 = {"name": "laptop22", "RAM": 64} myLaptop4 = {"name": "laptop124", "RAM": 4} myLaptop5 = {"name": "laptop34", "RAM": 8} laptops = [myLaptop1, myLaptop2, myLaptop3, myLaptop4, myLaptop5] for laptop in laptops: if laptop["name"] == "laptop1": print("laptop1 has RAM of {} GB before updating.".format(laptop["RAM"])) for laptop in laptops: if laptop["name"] == "laptop1": laptop["RAM"] = 32 print("Updated the value of RAM in laptop1") for laptop in laptops: if laptop["name"] == "laptop1": print("laptop1 has RAM of {} GB after updating.".format(laptop["RAM"])) |
Output:
1 2 3 4 5 |
laptop1 has RAM of 16 GB before updating. Updated the value of RAM in laptop1 laptop1 has RAM of 32 GB after updating. |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
def getVal(laptop): return laptop["RAM"] myLaptop1 = {"name": "laptop1", "RAM": 16} myLaptop2 = {"name": "laptop12", "RAM": 32} myLaptop3 = {"name": "laptop22", "RAM": 64} myLaptop4 = {"name": "laptop124", "RAM": 4} myLaptop5 = {"name": "laptop34", "RAM": 8} laptops = [myLaptop1, myLaptop2, myLaptop3, myLaptop4, myLaptop5] print("Order of laptops before sorting according to RAM.") for laptop in laptops: print(laptop["name"]) laptops.sort(key=getVal) print("Order of laptops after sorting according to RAM.") for laptop in laptops: print(laptop["name"]) |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Order of laptops before sorting according to RAM. laptop1 laptop12 laptop22 laptop124 laptop34 Order of laptops after sorting according to RAM. laptop124 laptop34 laptop1 laptop12 laptop22 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
myLaptop1 = {"name": "laptop1", "RAM": 16} myLaptop2 = {"name": "laptop12", "RAM": 32} myLaptop3 = {"name": "laptop22", "RAM": 64} myLaptop4 = {"name": "laptop124", "RAM": 4} myLaptop5 = {"name": "laptop34", "RAM": 8} laptops = [myLaptop1, myLaptop2, myLaptop3, myLaptop4, myLaptop5] print("Order of laptops before sorting according to RAM.") for laptop in laptops: print(laptop["name"]) laptops.sort(key=lambda laptop: laptop["RAM"]) print("Order of laptops after sorting according to RAM.") for laptop in laptops: print(laptop["name"]) |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Order of laptops before sorting according to RAM. laptop1 laptop12 laptop22 laptop124 laptop34 Order of laptops after sorting according to RAM. laptop124 laptop34 laptop1 laptop12 laptop22 |
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!