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 Item
Next article icon

Python Remove Item from Dictionary by Key

Last Updated : 01 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Dictionaries in Python store data as key-value pairs. Often, we need to remove a specific key-value pair to modify or clean the dictionary. For instance, consider the dictionary d = {'a': 1, 'b': 2, 'c': 3}; we might want to remove the key 'b'. Let's explore different methods to achieve this.

Using del()

del() statement is the most efficient way to remove an item by its key. It directly deletes the specified key-value pair from the dictionary.

Python
d = {'a': 1, 'b': 2, 'c': 3}  # Remove the key 'b' del d['b']  # The updated dictionary print(d) 

Output
{'a': 1, 'c': 3} 

Explanation:

  • The del() statement identifies the key 'b' in the dictionary and removes its associated key-value pair.
  • This method is fast because it directly modifies the dictionary in place.

Let's explore some more ways and see how we can remove item from dictionary by key.

Table of Content

  • Using pop() Method
  • Using Dictionary Comprehension
  • Using popitem() with Manual Checks

Using pop() Method

pop() method removes the key-value pair associated with a specific key and returns its value.

Python
# Initialize the dictionary d = {'a': 1, 'b': 2, 'c': 3}  # Remove the key 'b' and store its value val = d.pop('b')  # The updated dictionary print(d)  print(val)   

Output
{'a': 1, 'c': 3} 2 

Explanation:

  • pop() removes the key 'b' from the dictionary and retrieves its value, which can be useful for further operations.
  • This method is slightly less efficient than del due to the return value.

Using Dictionary Comprehension

This method creates a new dictionary by filtering out the key to be removed using dictionary comprehension.

Python
d = {'a': 1, 'b': 2, 'c': 3}  # Remove the key 'b' using dictionary comprehension d = {k: v for k, v in d.items() if k != 'b'}  # The updated dictionary print(d) 

Output
{'a': 1, 'c': 3} 

Explanation:

  • This method iterates through the dictionary and includes only the key-value pairs where the key is not 'b'.
  • It is less efficient because it creates a new dictionary, requiring additional memory and processing time.

Using popitem() with Manual Checks

popitem() method removes the last inserted key-value pair from the dictionary. We can use this with manual checks to find and remove a specific key, although it is not recommended for targeted key removal.

Python
d = {'a': 1, 'b': 2, 'c': 3}  # Manually check and remove the desired key-value pair for key in list(d.keys()):     if key == 'b':         del d[key]  # The updated dictionary print(d) 

Output
{'a': 1, 'c': 3} 

Explanation:

  • This method involves iterating through the dictionary and manually removing the desired key.
  • It is inefficient and should only be used in scenarios where targeted methods are not an option.

Next Article
Python Remove Dictionary Item
author
vijayendraprasad
Improve
Article Tags :
  • Python
  • Python Programs
  • python-dict
Practice Tags :
  • python
  • python-dict

Similar Reads

  • Python - Remove Item from Dictionary
    There are situations where we might want to remove a specific key-value pair from a dictionary. For example, consider the dictionary d = {'x': 10, 'y': 20, 'z': 30}. If we need to remove the key 'y', there are multiple ways to achieve this. Let's discuss several methods to remove an item from a dict
    3 min read
  • Python Remove Item from Dictionary by Value
    We are given a dictionary and our task is to remove key-value pairs where the value matches a specified target. This can be done using various approaches, such as dictionary comprehension or iterating through the dictionary. For example: d = {"a": 10, "b": 20, "c": 10, "d": 30} and we have to remove
    3 min read
  • Remove Kth Key from Dictionary - Python
    We are given a dictionary we need to remove Kth key from the dictionary. For example, we are given a dictionary d = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3', 'key4': 'value4'} we need to remove the key2 so that the output should be {'key1': 'value1', 'key3': 'value3', 'key4': 'value4'}.
    3 min read
  • Python Remove Dictionary Item
    Sometimes, we may need to remove a specific item from a dictionary to update its structure. For example, consider the dictionary d = {'x': 100, 'y': 200, 'z': 300}. If we want to remove the item associated with the key 'y', several methods can help achieve this. Let’s explore these methods. Using po
    3 min read
  • Python Remove Key from Dictionary if Exists
    We are given a dictionary and a key and our task is to remove the key from the dictionary if it exists. For example, d = {"a": 10, "b": 20, "c": 30} and key to remove is "b" then output will be {"a": 10, "c": 30}. Using pop() pop() method allows us to remove a key from a dictionary while specifying
    2 min read
  • Remove Spaces from Dictionary Keys - Python
    Sometimes, the keys in a dictionary may contain spaces, which can create issues while accessing or processing the data. For example, consider the dictionary d = {'first name': 'Nikki', 'last name': 'Smith'}. We may want to remove spaces from the keys to standardize the dictionary, resulting in {'fir
    3 min read
  • Python - Remove Top level from Dictionary
    Sometimes, while working with Python Dictionaries, we can have nesting of dictionaries, with each key being single values dictionary. In this we need to remove the top level of dictionary. This can have application in data preprocessing. Lets discuss certain ways in which this task can be performed.
    3 min read
  • Python - Remove Multiple Keys from Dictionary
    We are given a dictionary and our task is to remove multiple keys from the dictionary. For example, consider a dictionary d = {'a': 1, 'b': 2, 'c': 3, 'd': 4} where we want to remove the keys 'b' and 'd', then the output will be {'a': 1, 'c': 3}. Let's explore different methods to remove multiple ke
    3 min read
  • Get Dictionary Value by Key - Python
    We are given a dictionary and our task is to retrieve the value associated with a given key. However, if the key is not present in the dictionary we need to handle this gracefully to avoid errors. For example, consider the dictionary : d = {'name': 'Alice', 'age': 25, 'city': 'New York'} if we try t
    3 min read
  • Python | Grouping dictionary keys by value
    While performing computations over dictionary, we can come across a problem in which we might have to perform the task of grouping keys according to value, i.e create a list of keys, it is value of. This can other in cases of organising data in case of machine learning. Let's discuss certain way in
    4 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