Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python - Remove Dictionary Key Words
Next article icon

Remove Key from Dictionary List - Python

Last Updated : 28 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.


Next Article
Python - Remove Dictionary Key Words
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python list-programs
Practice Tags :
  • python

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 co
    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
    3 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
  • Ways to remove duplicates from list in Python
    In this article, we'll learn several ways to remove duplicates from a list in Python. The simplest way to remove duplicates is by converting a list to a set. Using set()We can use set() to remove duplicates from the list. However, this approach does not preserve the original order. [GFGTABS] Python
    2 min read
  • How to Remove Item from a List in Python
    Lists in Python have various built-in methods to remove items such as remove, pop, del and clear methods. Removing elements from a list can be done in various ways depending on whether we want to remove based on the value of the element or index. The simplest way to remove an element from a list by
    3 min read
  • Python - Remove empty value types in dictionaries list
    Sometimes, while working with Python dictionaries, we require to remove all the values that are virtually Null, i.e does not hold any meaningful value and are to be removed before processing data, this can be an empty string, empty list, dictionary, or even 0. This has applications in data preproces
    7 min read
  • Remove Last Element from List in Python
    Given a list, the task is to remove the last element present in the list. For Example, given a input list [1, 2, 3, 4, 5] then output should be [1, 2, 3, 4]. Different methods to remove last element from a list in python are: Using pop() methodUsing Slicing TechniqueUsing del OperatorUsing Unpacking
    2 min read
  • Remove falsy values from a list in Python
    Removing falsy values from a list filters out unwanted values like None, False, 0 and empty strings, leaving only truthy values. It's useful for data cleaning and validation. Using List ComprehensionList comprehension is a fast and efficient way to remove falsy values from a list . It filters out va
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences