
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to remove a key from a python dictionary?
In python, a dictionary is an unordered collection of data which is used to store data values such as map unlike other datatypes that store only single values. Keys of a dictionary must be unique and of immutable data type such as Strings, Integers, and tuples, but the key values can be repeated and be of any type. Dictionaries are mutable, therefore keys can be added or removed even after defining a dictionary in python.
They are many ways to remove a key from a dictionary, following are few ways.
Using pop(key,d)
The pop(key, d) method returns the value of a key from a dictionary. It accepts two arguments: the key to be removed and an optional value to return if the key cannot be found.
Example 1
Following is an example of using the required key parameter to pop an element.
#creating a dictionary with key value pairs #Using the pop function to remove a key dict = {1: "a", 2: "b"} print(dict) dict.pop(1) #printing the dictionary after removing a key
Output
The following output is generated on executing the above program.
{1: 'a', 2: 'b'}
Example 2
In the following example, the popped key value is accessed by assigning the popped value to a variable.
#creating a dictionary with key value pairs #Using the pop function to remove a key dict = {1: "a", 2: "b"} print(dict) value=dict.pop(1) #printing the dictionary after removing a key print(dict) #printing the popped value print(value)
Output
The following output is generated on executing the above program.
{1: 'a', 2: 'b'} {2: 'b'} a
Example 3
The following is an example which shows the removal of a key from a dictionary using the del() function. Unlike pop() using dictionary, we can’t return any value using the del() function.
#creating a dictionary with key value pairs #Using the pop function to remove a key dict = {1: "a", 2: "b"} print(dict) del(dict[1]) #printing the dictionary after removing a key print(dict)
Output
The following output is generated on executing the above program.
{1: 'a', 2: 'b'} {2: 'b'}
Using Dict Comprehensions
The preceding techniques update a dictionary while it is still in use, which means the key value pair is deleted. If we need to keep the original key, we can do it with a custom function.
It's general knowledge in Python that list comprehensions can be used to construct a new list from an existing one. We can use dict comprehensions to perform the same thing with dictionaries. Instead of eliminating entries from a list, we can create a new dictionary with a condition that excludes the values we don't want using a dict comprehension.
Example
The following is an example in which a key is removed from a python dictionary using dict comprehensions.
dict = {1: "a", 2: "b"} #printing dictionary before deletion print(dict) dict2 = {k: i for k, i in dict.items() if k != 1} #printing new dictionary print("dictionary after dictionary comprehension") print(dict2)
Output
The following output is generated on executing the above program.
{1: 'a', 2: 'b'} dictionary after dictionary comprehension {2: 'b'}
- Related Articles
- Python - Ways to remove a key from dictionary
- How to get a value for a given key from a Python dictionary?
- Python Program to remove duplicate elements from a dictionary
- How to check if a key exists in a Python dictionary?
- How to print a value for a given key for Python dictionary?
- Accessing Key-value in a Python Dictionary
- How to extract subset of key-value pairs from Python dictionary object?
- Add a key value pair to dictionary in Python
- Java Program to remove a key from a HashMap
- How to check if a given key already exists in a Python dictionary?
- How to update the value of a key in a dictionary in Python?
- Get key from value in Dictionary in Python
- How to truncate Key Length in Python Dictionary?
- How to search Python dictionary for matching key?
- Remove elements from a Dictionary using Javascript
