Open In App

Remove Dictionary from List If Key is Equal to Value in Python

Last Updated : 09 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Removing dictionaries from a list based on a specific condition is a common task in Python programming. This operation is useful when working with data represented as a list of dictionaries, and it allows for a more streamlined and refined dataset. In this article, we will explore three concise methods to achieve this task using Python

Example:

Input: [{'geeks':'geeks','letters':4}, {'name':'gfg', 3:3}, {'name':'geeksforgeek', 'letters':12}] 
Output: [{'name': 'geeksforgeek', 'letters': 12}]

Remove Dictionary From List If Key Is Equal To Value

Below, are the methods for Removing Dictionary From List If Key Is Equal To Value in Python.

Remove Dictionary From List If Key Is Equal To Value Using List Comprehension

In this example, The given code defines a list of dictionaries, named `list_dictionary`. It then uses a list comprehension to create a new list, `result`, by filtering out dictionaries where any key is equal to its corresponding value. Finally, the resulting list is printed.

Python3




# Input
list_dictionary = [{'geeks':'geeks','letters':4},
                    {'name':'gfg', 3:3},
                    {'name':'geeksforgeek', 'letters':12}]
 
# Remove dictionaries where key is equal to value
result = [d for d in list_dictionary if not any(k == v for k, v in d.items())]
 
# Output
print(result)


Output

[{'name': 'geeksforgeek', 'letters': 12}]



Remove Dictionary From List If Key Is Equal To Value Using Filter() Function

In this example, below code initializes a list of dictionaries named `list_dictionary`. It then utilizes the `filter()` function along with a lambda function to create a new list, `result`, excluding dictionaries where any key is equal to its corresponding value.

Python3




# Input
list_dictionary = [{'geeks':'geeks','letters':4},
                    {'name':'gfg', 3:3},
                    {'name':'geeksforgeek', 'letters':12}]
 
# Remove dictionaries where key is equal to value
result = list(filter(lambda d: not any(k == v for k, v in d.items()), list_dictionary))
 
# Output
print(result)


Output

[{'name': 'geeksforgeek', 'letters': 12}]



Remove Dictionary From List If Key Is Equal To Value Using all() Function

dIn this example, below code initializes a list of dictionaries named `list_dictionary`. It uses a list comprehension to create a new list, `result`, excluding dictionaries where all keys are equal to their corresponding values. The resulting list is then printed.

Python3




# Input
list_dictionary = [{'geeks':'geeks','letters':4},
                    {'name':'gfg', 3:3},
                    {'name':'geeksforgeek', 'letters':12}]
 
# Remove dictionaries where key is equal to value
result = [d for d in list_dictionary if all(k != v for k, v in d.items())]
 
# Output
print(result)


Output

[{'name': 'geeksforgeek', 'letters': 12}]



Conclusion

In conclusion above, methods provide different ways to achieve the same goal, allowing you to choose the one that fits your coding style or specific requirements. Whether you prefer the simplicity of list comprehensions or the flexibility of higher-order functions, Python provides the tools to express these operations in a readable and efficient manner.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads