Home  >  Article  >  Backend Development  >  Python - Remove dictionary from list of dictionaries

Python - Remove dictionary from list of dictionaries

WBOY
WBOYforward
2023-08-19 21:49:041122browse

Python - 从字典列表中删除字典

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 of

Example

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 of

Example

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 of

Example

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 of

Example

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 of

Example

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete