VOOZH about

URL: https://www.geeksforgeeks.org/python/remove-dictionary-from-list-if-key-is-equal-to-value-in-python/

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


  • Courses
  • Tutorials
  • Interview Prep

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

Last Updated : 23 Jul, 2025

Removing dictionaries from a list based on a specific condition is a common task in Python, especially when working with data in list-of-dictionaries format. In this article, we will see various methods to Remove Dictionary from List If the Key is Equal to the Value in Python.

Using filter()

filter()remove dictionaries where any key equals its value, then sort the remaining dictionaries in descending order based on the 'letters' key.


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

Explanation:

  • This removes dictionaries where any key equals its value.
  • sorts the remaining dictionaries by the 'letters' key in descending order.

Using List Comprehension

list comprehension filter out dictionaries where any key equals its value, then sorts the remaining dictionaries in descending order based on the 'letters' key.


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

Explanation:

  • Removes dictionaries where any key equals its value.
  • Returns dictionaries where no key equals its value.

Using for loop

This for loop modifies the list directly, saving memory, but can be slower due to in-place deletion, especially with large lists.


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

Explanation:

  • This iterates through a and removes dictionaries where a key equals its value.
  • Modifies the list in-place by deleting matching dictionaries.
Comment