You are currently viewing List of Dictionaries in Python

We are often required to use a dictionary as an element in the Python List, this is referred to as a list of dictionaries. In this article, we will discuss how to create a list of dictionaries, insert, update, delete, and select data from a list of dictionaries in python.

Advertisements

We can create a list of dictionaries by specifying dictionaries with elements in a list. Each dictionary is separated by a comma within a list.

By using the index position, we can retrieve a specific dictionary from the list. Along with the index position we can specify the dictionary key to return a particular value.

To insert a new dictionary into a list of existing dictionaries, we can utilize the list.append() method, this will insert a new dictionary at the end.

By using the index itself, we can assign a new value to the existing dictionary keys. In this way, we can update data in the list of dictionaries.

del keyword can be utilized to delete a particular dictionary from the list of dictionaries.

To sort list of Python dictionaries in ascending/descending order, we can use Python sorted() method.

1. Quick Examples of CRUD Operations on List of Dictionaries

Following is an example that will perform the create, select, update, and delete dictionary element from the list of dictionaries in Python.


# Quick Examples of CRUD Operations on List of Dictionaries

# Consider the employee details
# Stored as list of dictionaries
employee = [
    {'Name':'Sravan','Salary':48000,'age':23},
    {'Name':'Raghu','Salary':25000,'age':21},
    {'Name':'Ram','Salary':21000,'age':23},
    {'Name':'Shivani','Salary':148000,'age':31},
    {'Name':'Salma','Salary':41000,'age':22}]

# Return the dictionary present at index position-2  
print(employee[4])

# Return the Salary present at index position-4 from dictionary 
value = employee[4]['Salary']

# Update Salary in dictionary present at 1st position.
employee[1]['Name'] = 'Gita'

# Delete dictionary at index-2 in the list.
del employee[2]

# Sort list of dictionaries using sorted() methods
print(sorted(employee, key=lambda x: x['salary']))

# Sort list of dictionaries using sorted() methods in descending order
print(sorted(employee, key=lambda x: x['salary'], reverse = True))

2. Create a List of Dictionaries

The dictionary is created using {} and the List is created using []. So by passing {} in a list, we can create a list of dictionaries in Python. Let’s see how to create a list of dictionaries in python with an example.

In this example, we will create 5 dictionaries in a list such that each dictionary have 3 items.


# Consider the employee details
# Stored as List of dictionaries
employee = [
    {'Name':'Sravan','Salary':48000,'age':23},
    {'Name':'Raghu','Salary':25000,'age':21},
    {'Name':'Ram','Salary':21000,'age':23},
    {'Name':'Shivani','Salary':148000,'age':31},
    {'Name':'Salma','Salary':41000,'age':22}]

print(employee)

# Output:
# [{'Name': 'Sravan', 'Salary': 48000, 'age': 23}, {'Name': 'Raghu', 'Salary': 25000, 'age': 21}, {'Name': 'Ram', 'Salary': 21000, 'age': 23}, {'Name': 'Shivani', 'Salary': 148000, 'age': 31}, {'Name': 'Salma', 'Salary': 41000, 'age': 22}]

The keys in the dictionary are ‘Name‘, ‘Salary‘ and ‘age‘.

3. Select a Dictionary from the List of Python dictionaries

By using the index position, we can select a particular dictionary from the list of Python dictionaries. Indexing starts from 0. If you want to select only a particular key from a dictionary, you need to specify the key along with the index position.

Let’s see how to return a particular dictionary from a list of dictionaries.


# Here, list_of_dictionaries is the input and index_pos is the index value

# Return particular dictionary
list_of_dictionaries[index_pos]

# Return particular key from the dictionary in list_of_dictionaries 
list_of_dictionaries[index_pos]['key']

Example 1:

  1. dictionary present at index position-2.
  2. dictionary present at index position-4

# Return the dictionary present at index position-2  
print(employee[2])

# Return the dictionary present at index position-4
print(employee[4])

# Output:
# {'Name': 'Ram', 'Salary': 21000, 'age': 23}
# {'Name': 'Salma', 'Salary': 41000, 'age': 22}

The dictionaries present at index-2 and 4 were returned.

Example 2: Let’s return the

  1. Name present at index position-2 from dictionary.
  2. Salary present at index position-4 from dictionary.

# Return the Name present at index position-2 from dictionary 
print(employee[2]['Name'])

# Return the Salary present at index position-4 from dictionary 
print(employee[4]['Salary'])

# Output:
# Ram
# 41000

The Name in the 5th dictionary is ‘Ram’ and the Salary in the 3rd dictionary is 41000.

4. Insert a Python Dictionary to List of Dictionaries

list.append() will insert the dictionary at the end of the input list of dictionaries. We need to specify the entire dictionary inside the append method.

Let’s add the dictionary – {‘Name’: ‘Uday’, ‘Salary’: 42000, ‘age’: 22} to the employee. This Python dictionary is added at the end of the list.


# Append one employee detail to a list
employee.append({'Name': 'Uday', 'Salary': 42000, 'age': 22})
print(employee)

# Output:
# [{'Name': 'Sravan', 'Salary': 48000, 'age': 23}, {'Name': 'Raghu', 'Salary': 25000, 'age': 21}, {'Name': 'Ram', 'Salary': 21000, 'age': 23}, {'Name': 'Shivani', 'Salary': 148000, 'age': 31}, {'Name': 'Salma', 'Salary': 41000, 'age': 22}, {'Name': 'Uday', 'Salary': 42000, 'age': 22}]

5. Update the Python Dictionary in List

Let’s update the Salary in the dictionary present at the 3rd position to 0 and the Name in the dictionary present at the 1st position to ‘Gita’.


# Update Salary in dictionary present at 3rd position.
employee[3]['Salary'] = 0

print(employee[3]['Salary'] )

# Update Name in dictionary present at 1st position.
employee[1]['Name'] = 'Gita'

print(employee[1]['Name'])

# Output:
# 0
# Gita

Previously, the Salary in the dictionary present at the 3rd position is 148000 and now we updated it to 0, the Name in the dictionary present at the 1st position is ‘Raghu’ and now we updated it to ‘Gita’.

6. Delete Dictionary from the List of Dictionaries

Using the del keyword to delete a particular dictionary from a list of dictionaries in Python. By using the index position we can delete a particular dictionary. Let’s see how to delete a dictionary from a list of dictionaries.


del list_of_dictionaries[index_pos]

Let’s delete the dictionary present at index position-2.


# Delete dictionary at index-2 in the list.
del employee[2]
print(employee)

# Output:
# [{'Name': 'Sravan', 'Salary': 48000, 'age': 23}, {'Name': 'Raghu', 'Salary': 25000, 'age': 21}, {'Name': 'Shivani', 'Salary': 148000, 'age': 31}, {'Name': 'Salma', 'Salary': 41000, 'age': 22}]

The dictionary element at index pos 2 is deleted.

7. Sort List of Python Dictionaries

Python sorted() function can be used to sort the list of dictionaries in ascending/descending order. For that, we need to pass the given list of dictionaries and key param(it specifies to sort the list by value using lambda function) into sorted() function. It will sort the list of dictionaries in ascending order.


# Sort list of dictionaries using sorted() & lambda
# according to 'salary' 
print(sorted(employee, key=lambda x: x['salary']))

# Output:
# [{'Name': 'Ram', 'Salary': 21000, 'age': 23}, {'Name': 'Raghu', 'Salary': 25000, 'age': 21}, {'Name': 'Salma', 'Salary': 41000, 'age': 22}, {'Name': 'Sravan', 'Salary': 48000, 'age': 23}, {'Name': 'Shivani', 'Salary': 148000, 'age': 31}]

7.1 Sort List of Dictionaries in Descending Order

We can also sort the list of dictionaries in descending order using the sorted() method. For that, we need to set the reverse param as True and then pass it into the sorted() method along with a given list of dict and key param. It will sort the list of dictionary in decending order.


# Sort list of dictionaries using sorted() & lambda
# according to 'salary' in descending order
print(sorted(employee, key=lambda x: x['salary'], reverse = True))

# Output:
# [{'Name': 'Shivani', 'Salary': 148000, 'age': 31}, {'Name': 'Sravan', 'Salary': 48000, 'age': 23}, {'Name': 'Salma', 'Salary': 41000, 'age': 22}, {'Name': 'Raghu', 'Salary': 25000, 'age': 21}, {'Name': 'Ram', 'Salary': 21000, 'age': 23}]

8. Conclusion

In this article, you have learned how to perform different operations on the list of dictionaries in Python for example, how to create, select, update, insert, sort and delete dictionaries from the list of dictionaries. For the select, update, and delete operations, we utilized the index position. Insert operation is performed with the help of list.append() method.