Remove Dictionary from List If Key is Equal to Value in Python Last Updated : 29 Dec, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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. Python a= [{'geeks':'geeks','letters':4}, {'name':'gfg', 3:3}, {'name':'geeksforgeek', 'letters':12}] # Remove dictionaries where key is equal to value res= list(filter(lambda d: not any(k == v for k, v in d.items()), list_dictionary)) print(res) 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 Comprehensionlist comprehension filter out dictionaries where any key equals its value, then sorts the remaining dictionaries in descending order based on the 'letters' key. Python a= [{'geeks':'geeks','letters':4}, {'name':'gfg', 3:3}, {'name':'geeksforgeek', 'letters':12}] # Remove dictionaries where key is equal to value res = [d for d in list_dictionary if not any(k == v for k, v in d.items())] print(res) 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. Python a = [{'geeks': 'geeks', 'letters': 4}, {'name': 'gfg', 3: 3}, {'name': 'geeksforgeek', 'letters': 12}] # Iterate over the list and remove dictionaries where any key equals its value i = 0 while i < len(a): if any(k == v for k, v in a[i].items()): del a[i] else: i += 1 print(a) 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 More infoAdvertise with us Next Article Remove Dictionary from List If Key is Equal to Value in Python G gangarajul8khm Follow Improve Article Tags : Python Python Programs python-list Python dictionary-programs Practice Tags : pythonpython-list Similar Reads Python - Remove Dictionary if Given Key's Value is N We are given a dictionary we need to remove key if the given value of key is N. For example, we are given a dictionary d = {'a': 1, 'b': 2, 'c': 3} we need to remove the key if the value is N so that the output becomes {'a': 1, 'c': 3}. We can use methods like del, pop and various other methods like 2 min read Python - Remove K valued key from Nested Dictionary We are given a nested dictionary we need to remove K valued key. For example, we are given a nested dictionary d = { "a": 1, "b": {"c": 2,"d": {"e": 3,"f": 1},"g": 1},"h": [1, {"i": 1, "j": 4}]} we need to remove K valued key ( in our case we took k value as 1 ) from it so that the output should be 3 min read Remove K Value Items from Dictionary Nesting - Python We are given a dictionary we need to remove K value items from dictionary. For example we are having a dictionary d = {'a': {'b': 'remove', 'c': 'keep'}, 'd': {'e': 'remove', 'f': 'keep'}} we need to remove the K value suppose in this case K is 'remove' so that output should be {'a': {'c': 'keep'}, 3 min read Remove Nth Element from Kth key's Value from the Dictionary - Python We are given a dictionary we need to remove the Nth element from the Kth key value. For example we are given a dictionary d = {'key1': ['a', 'b', 'c', 'd'], 'key2': ['w', 'x', 'y', 'z']} we need to remove the Nth element from the Kth key value so that the output should become {'key1': ['a', 'b', 'd' 3 min read How to Remove Key-Value Pair from a JSON File in Python We are given a Python JSON file and our task is to remove a key-value pair in a JSON file in Python. In this article, we will see how we can remove a key-value pair in a JSON file in Python. Remove Key-Value Pair from a JSON File in PythonBelow are the possible approaches to Remove items from a JSON 5 min read Like