Python – Remove Key from Dictionary List

Sometimes, while working with Python dictionaries, we can have a problem in which we need to remove a specific key from a dictionary list. This kind of problem is very common and has application in almost all domains includeing day-day programming and web development domain. Lets discuss certain ways in which this task can be performed.

Method #1 : Using loop + del
The combination of above functions can be used to solve this problem. In this, we iterate for all the keys and delete the required key from each dictionary using del.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Remove Key from Dictionary List
# Using loop + del
  
# initializing list
test_list = [{'Gfg' 1, 'id' : 2, 'best' : 8 }, 
             {'Gfg' 4, 'id' : 4, 'best'10}, 
             {'Gfg' 4, 'id' : 8, 'best'11}]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing key 
del_key = 'id'
  
# Remove Key from Dictionary List
# Using loop + del
for items in test_list:
    if del_key in items:
        del items[del_key]
  
# printing result 
print("The modified list : " + str(test_list)) 

chevron_right


Output :

The original list is : [{'best': 8, 'id': 2, 'Gfg': 1}, {'best': 10, 'id': 4, 'Gfg': 4}, {'best': 11, 'id': 8, 'Gfg': 4}]
The modified list : [{'best': 8, 'Gfg': 1}, {'best': 10, 'Gfg': 4}, {'best': 11, 'Gfg': 4}]

 

Method #2 : Using list comprehnsion + dictionary comprehension
This is yet another way in which this task can be performed. In this, we reconstruct each dictionary removing out the specific key from it.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Remove Key from Dictionary List
# Using list comprehnsion + dictionary comprehension
  
# initializing list
test_list = [{'Gfg' 1, 'id' : 2, 'best' : 8 }, 
             {'Gfg' 4, 'id' : 4, 'best'10}, 
             {'Gfg' 4, 'id' : 8, 'best'11}]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing key 
del_key = 'id'
  
# Remove Key from Dictionary List
# Using list comprehnsion + dictionary comprehension
res = [{key : val for key, val in sub.items() if key != del_key} for sub in test_list]
  
# printing result 
print("The modified list : " + str(res)) 

chevron_right


Output :

The original list is : [{'best': 8, 'id': 2, 'Gfg': 1}, {'best': 10, 'id': 4, 'Gfg': 4}, {'best': 11, 'id': 8, 'Gfg': 4}]
The modified list : [{'best': 8, 'Gfg': 1}, {'best': 10, 'Gfg': 4}, {'best': 11, 'Gfg': 4}]



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.