Remove Key from Dictionary List - Python
Last Updated :
28 Jan, 2025
We are given a list of dictionaries and our task is to remove a specific key from each dictionary in the list. For example, if we have the following list: li = [{'Gfg': 1, 'id': 2, 'best': 8}, {'Gfg': 4, 'id': 4, 'best': 10}, {'Gfg': 4, 'id': 8, 'best': 11}] and the key to remove is "id" then the resulting list will be [{'Gfg': 1, 'best': 8}, {'Gfg': 4, 'best': 10}, {'Gfg': 4, 'best': 11}].
Using loop and del
We can iterate through the list of dictionaries and use the del keyword to remove the specified key from each dictionary and this approach directly modifies the original list.
Python
li = [{'Gfg': 1, 'id': 2, 'best': 8},
{'Gfg': 4, 'id': 4, 'best': 10},
{'Gfg': 4, 'id': 8, 'best': 11}]
del_key = 'id'
# Removing the key using loop + del
for item in li:
if del_key in item:
del item[del_key]
print(li)
Output[{'Gfg': 1, 'best': 8}, {'Gfg': 4, 'best': 10}, {'Gfg': 4, 'best': 11}]
Explanation: Each dictionary in the list is checked for the key and if it exists then the del statement removes the key-value pair.
Using pop()
We use the pop() method to remove the key from each dictionary in the list and if the key does not exist then we handle it by specifying a default value.
Python
li = [{'Gfg': 1, 'id': 2, 'best': 8},
{'Gfg': 4, 'id': 4, 'best': 10},
{'Gfg': 4, 'id': 8, 'best': 11}]
del_key = 'id'
# Removing the key using pop()
for item in li:
item.pop(del_key, None)
print(li)
Output[{'Gfg': 1, 'best': 8}, {'Gfg': 4, 'best': 10}, {'Gfg': 4, 'best': 11}]
Explanation: pop() method removes the key-value pair if the key exists in the dictionary and if the key does not exist then the default value None prevents a KeyError.
Using Dictionary Comprehension
This method involves reconstructing each dictionary in the list excluding the specified key using a dictionary comprehension.
Python
li = [{'Gfg': 1, 'id': 2, 'best': 8},
{'Gfg': 4, 'id': 4, 'best': 10},
{'Gfg': 4, 'id': 8, 'best': 11}]
del_key = 'id'
# Removing the key using dictionary comprehension
li = [{k: v for k, v in item.items() if k != del_key} for item in li]
print(li)
Output[{'Gfg': 1, 'best': 8}, {'Gfg': 4, 'best': 10}, {'Gfg': 4, 'best': 11}]
Explanation: dictionary comprehension {k: v for k, v in item.items() if k != del_key} creates a new dictionary excluding the key del_key.
Using map() and a Lambda Function
In this method we use map() function combined with a lambda function to remove the specified key from each dictionary in the list.
Python
li = [{'Gfg': 1, 'id': 2, 'best': 8},
{'Gfg': 4, 'id': 4, 'best': 10},
{'Gfg': 4, 'id': 8, 'best': 11}]
del_key = 'id'
# Removing the key using map() and a lambda function
li = list(map(lambda item: {k: v for k, v in item.items() if k != del_key}, li))
print(li)
Output[{'Gfg': 1, 'best': 8}, {'Gfg': 4, 'best': 10}, {'Gfg': 4, 'best': 11}]
Explanation: map() function applies the lambda function to each item in the list and the lambda function {k: v for k, v in item.items() if k != del_key} removes the specified key from each dictionary.
Similar Reads
Python - Remove last element from dictionary Given a dictionary, the task is to remove the last occurring element, i.e. the last key-value pair in the dictionary. Example: Input: {1: 'Geeks', 2: 'For', 3: 'Geeks'} Output:{1: 'Geeks', 2: 'For'}Method 1: Using popitem() method Python dictionary popitem() method removes one key, value pair as a t
5 min read
Python - Ways to remove a key from dictionary We are given a dictionary and our task is to remove a specific key from it. For example, if we have the dictionary d = {"a": 1, "b": 2, "c": 3}, then after removing the key "b", the output will be {'a': 1, 'c': 3}.Using pop()pop() method removes a specific key from the dictionary and returns its cor
3 min read
Python - Remove Dictionary Key Words We are given a dictionary we need to remove the dictionary key words. For example we are given a dictionary d = {'a': 1, 'b': 2} we need to remove the dictionary key words so that the output becomes {'b': 2} . We can use method like del , pop and other methods to do it.Using del keyworddel keyword i
2 min read
Python - Remove item from dictionary when key is unknown We are given a dictionary we need to remove the item or the value of key which is unknown. For example, we are given a dictionary a = {'a': 10, 'b': 20, 'c': 30} we need to remove the key 'b' so that the output dictionary becomes like {'a': 10, 'c': 30} . To do this we can use various method and app
4 min read
Remove first element from list in Python The task of removing the first element from a list in Python involves modifying the original list by either deleting, popping, or slicing the first element. Each method provides a different approach to achieving this. For example, given a list a = [1, 2, 3, 4], removing the first element results in
2 min read