
- 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
Python - Ways to remove a key from dictionary
Dictionary is used in manifold practical applications such as day-day programming, web development and AI/ML programming as well, making it a useful container overall. Hence, knowing the ways for achieving different tasks related to dictionary usage always is a plus.
Example
# using del # Initializing dictionary test_dict = {"Vishesh" : 29, "Ram" : 21, "Vishal" : 27, "Prashant" : 25} # Printing dictionary before removal print ("The dictionary before performing remove is : " + str(test_dict)) # Using del to remove a dict del test_dict['Vishal'] # Printing dictionary after removal print ("The dictionary after remove is : " + str(test_dict)) # using pop() # Initializing dictionary test_dict = {"Vishesh" : 29, "Ram" : 21, "Vishal" : 27, "Prashant" : 25} # Printing dictionary before removal print ("The dictionary before performing remove is : " + str(test_dict)) # Using pop() to remove a dict. pair removed_value = test_dict.pop('Ram') # Printing dictionary after removal print ("The dictionary after remove is : " + str(test_dict)) print ("The removed key's value is : " + str(removed_value)) # Using pop() to remove a dict. pair doesn't raise exception # assigns 'No Key found' to removed_value removed_value = test_dict.pop('Nilesh', 'No Key found') # Printing dictionary after removal print ("The dictionary after remove is : " + str(test_dict)) print ("The removed key's value is : " + str(removed_value)) # using items() + dict comprehension # Initializing dictionary test_dict = {"Vishesh" : 29, "Ram" : 21, "Vishal" : 27, "Prashant" : 25} # Printing dictionary before removal print ("The dictionary before performing remove is : " + str(test_dict)) # Using items() + dict comprehension to remove a dict. pair new_dict = {key:val for key, val in test_dict.items() if key != 'Prashant} # Printing dictionary after removal print ("The dictionary after remove is : " + str(new_dict)) '
- Related Articles
- How to remove a key from a python dictionary?
- Python Program to remove duplicate elements from a dictionary
- Python - Ways to remove duplicates from list
- Python - Ways to Copy Dictionary
- Get key from value in Dictionary in Python
- How to get a value for a given key from a Python dictionary?
- Ways to copy dictionary in python
- Python - Ways to create a dictionary of Lists
- Python – Concatenate Tuple to Dictionary Key
- Ways to create a dictionary of lists in python
- Python - Ways to invert mapping of dictionary
- Accessing Key-value in a Python Dictionary
- Add a key value pair to dictionary in Python
- How to extract subset of key-value pairs from Python dictionary object?
- What is the best way to remove an item from a Python dictionary?

Advertisements