Python - Removing Dictionary from list of Dictionaries


Dictionary is a very commonly used feature of python which is used to store data within it as per user need. Another typical procedure involves editing or manipulating this data. To become a productive and quick programmer, you have to figure out how to get rid of a dictionary from a list of dictionaries. There are many techniques for removing a dictionary from the list of dictionaries, which are going to be covered in this article.

Different Methods to Remove Dictionary from List of Dictionaries

Loop Method

We will specify the dictionary which is to be removed from the list of dictionaries and then we will create a condition using if() to provide an argument to remove the dictionary from the list of dictionaries. We can understand in clearer way through the following example:

Example

# Dictionaries
Cities = [
    {"City": "Bangalore", "location": "India"},
    {"City": "Toronto", "location": "Canada"},
    {"City": "Liverpool", "location": "England"},
    {"City": "kano", "location": "Nigeria"},
    {"City": "Sydney", "location": "Australia"},
    {"City": "Berlin", "location": "Germany"},
    {"City": "New York", "location": "USA"}
]

Remove = "Liverpool"  #Specifying the dictionary to be removed

for city in Cities:  # Checking all the different dictionaries
    if city["City"] == Remove: #Creating a condition 
        Cities.remove(city)   #If the condition is satisfied remove() method will be used

print(Cities)  #Display the output after removing the dictionary

Output

The output of the program will be as follows:

[{'City': 'Bangalore', 'location': 'India'}, {'City': 'Toronto', 'location': 'Canada'}, {'City': 'kano', 'location': 'Nigeria'}, {'City': 'Sydney', 'location': 'Australia'}, {'City': 'Berlin', 'location': 'Germany'}, {'City': 'New York', 'location': 'USA'}]  

List Comprehension

By using the list comprehension method, we will remove the specific dictionary by applying condition and then we can create a new list of the modified list of dictionaries without the presence of the specified dictionary. We can understand it more clearly through the following example:

Example

#Dictionaries
Cities = [
    {"City": "Bangalore", "location": "India"},
    {"City": "Toronto", "location": "Canada"},
    {"City": "Liverpool", "location": "England"},
    {"City": "kano", "location": "Nigeria"},
    {"City": "Sydney", "location": "Australia"},
    {"City": "Berlin", "location": "Germany"},
    {"City": "New York", "location": "USA"}
]

Remove = "Liverpool"  #Specifying Dictionary To Be Removed

Cities = [city for city in Cities if city["City"] != Remove]  #Creating a new list and specifying the condition to remove the unwanted dictionary

print(Cities)  #Display The Updated Output

Output

The output of the above program will be as follows:

[{'City': 'Bangalore', 'location': 'India'}, {'City': 'Toronto', 'location': 'Canada'}, {'City': 'kano', 'location': 'Nigeria'}, {'City': 'Sydney', 'location': 'Australia'}, {'City': 'Berlin', 'location': 'Germany'}, {'City': 'New York', 'location': 'USA'}]  

Changing Original List

In this method we don’t create any new list and instead make changes in the original list of dictionaries directly. So, this makes the work easy and fast and also duplication of data is not done. We can understand it more clearly through the following example:

Example

# Dictionaries
Cities = [
    {"City": "Bangalore", "location": "India"},
    {"City": "Toronto", "location": "Canada"},
    {"City": "Liverpool", "location": "England"},
    {"City": "kano", "location": "Nigeria"},
    {"City": "Sydney", "location": "Australia"},
    {"City": "Berlin", "location": "Germany"},
    {"City": "New York", "location": "USA"}
]

for City in Cities:  #We will specify a condition
    if City.get("location") == 'England':   #If the location is England
        Cities.remove(City)  #Remove the dictionary with location as England

print(Cities) #Display The Modified Output

Output

The output of the above code will be as follows:

[{'City': 'Bangalore', 'location': 'India'}, {'City': 'Toronto', 'location': 'Canada'}, {'City': 'kano', 'location': 'Nigeria'}, {'City': 'Sydney', 'location': 'Australia'}, {'City': 'Berlin', 'location': 'Germany'}, {'City': 'New York', 'location': 'USA'}] 

Filter Function

As the name suggests we will simply apply a filter to specify the dictionary to be removed from the list of dictionaries. We can understand it in a better way through the following example:

Example

#Dictionaries
Cities = [
    {"City": "Bangalore", "location": "India"},
    {"City": "Toronto", "location": "Canada"},
    {"City": "Liverpool", "location": "England"},
    {"City": "kano", "location": "Nigeria"},
    {"City": "Sydney", "location": "Australia"},
    {"City": "Berlin", "location": "Germany"},
    {"City": "New York", "location": "USA"}
]

new_dictionary = list(filter(lambda City: City.get("location") != 'England', Cities))  # We specified a condition that if the location is England is found from the list then it is to be filtered out and removed from the list of dictionaries

print(new_dictionary)  #Display the Modified Output

Output

The output of the above program will be as follows:

[{'City': 'Bangalore', 'location': 'India'}, {'City': 'Toronto', 'location': 'Canada'}, {'City': 'kano', 'location': 'Nigeria'}, {'City': 'Sydney', 'location': 'Australia'}, {'City': 'Berlin', 'location': 'Germany'}, {'City': 'New York', 'location': 'USA'}]   

List Indexing

This method is used only in the case of small list of dictionaries when you know the exact position of the dictionary to be removed. So you just have to specify the position of the dictionary to remove it. Lets take an example to understand it more clearly:

Example

#Dictionaries
Cities = [
    {"City": "Bangalore", "location": "India"},
    {"City": "Toronto", "location": "Canada"},
    {"City": "Liverpool", "location": "England"},
    {"City": "kano", "location": "Nigeria"},
    {"City": "Sydney", "location": "Australia"},
    {"City": "Berlin", "location": "Germany"},
    {"City": "New York", "location": "USA"}
]

dictionary_remove= 2  #It specifies the position of the dictionary to be removed
#The index number starts from 0
del Cities[dictionary_remove]  #It commands to delete the dictionary in specified index number

print(Cities)  #Displays the Modified Output

Output

The output of the above program will be as follows:

[{'City': 'Bangalore', 'location': 'India'}, {'City': 'Toronto', 'location': 'Canada'}, {'City': 'kano', 'location': 'Nigeria'}, {'City': 'Sydney', 'location': 'Australia'}, {'City': 'Berlin', 'location': 'Germany'}, {'City': 'New York', 'location': 'USA'}]  

Conclusion

When working with vast amounts of data, altering the data is a necessary step in the process. Therefore, it is essential to be aware about various techniques in order to quickly implement modifications.

This article details every possible way to remove a dictionary from a list of dictionaries that is included in a data source. When working on this type of approach, you must be attentive since there is a possibility that there will be data errors that might result in the loss of your data. As a result, it is essential to make a backup of your data prior to implementing any changes to it.

Updated on: 01-Aug-2023

90 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements