Python - Remove dictionary from list of dictionaries
Dictionary is a commonly used feature in Python for storing data as per the user’s needs. Another typical process involves editing or manipulating this data. To become an efficient and fast programmer, you have to figure out how to remove a dictionary from the list of dictionaries. This article will cover many techniques for removing dictionaries from the dictionary list.
Different ways to remove a dictionary from a list of dictionaries
Loop method
We will specify the dictionary to be removed from the list of dictionaries, and then we will create a condition using if() to provide a parameter to remove the dictionary from the list of dictionaries. We can understand more clearly with the following example:
The Chinese translation ofExample
is: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 look like this:
[{'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 can remove a specific dictionary by applying a condition, and then we can create a modified list of dictionaries that does not contain the specified dictionary. We can understand more clearly with the following example:
The Chinese translation ofExample
is: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 look like this:
[{'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'}]
Change original list
In this method, we do not create any new list, but directly make changes in the original dictionary list. Therefore, it is easy and fast to do so without duplication of data. We can understand more clearly with the following example:
The Chinese translation ofExample
is: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 is 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 which dictionaries to remove from the list of dictionaries. We can understand better with the following example:
The Chinese translation ofExample
is: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 look like this:
[{'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 index
This method is only used when the list of dictionaries is small and you know the exact position of the dictionary you want to delete. Therefore, you only need to specify the location of the dictionary you want to delete. Let us take an example to understand more clearly:
The Chinese translation ofExample
is: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 look like this:
[{'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'}]
in conclusion
When processing large amounts of data, modifying the data is a necessary step. Therefore, it is important to understand the various techniques in order to implement modifications quickly.
This article details all possible ways to remove a dictionary from the list of dictionaries contained in a data source. You must remain vigilant when performing this type of operation as data errors may occur, possibly resulting in data loss. Therefore, it is necessary to back up your data before making any changes to it.
The above is the detailed content of Python - Remove dictionary from list of dictionaries. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

Regular expressions are powerful tools for pattern matching and text manipulation in programming, enhancing efficiency in text processing across various applications.

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...
