
- 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
Delete items from dictionary while iterating in Python
A python dictionary is a collection which is unordered, changeable and indexed. They have keys and values and each item is referred using the key. In this article we will explore the ways to delete the items form a dictionary.
Using del with keys
In this approach we capture the key values that are needed to be deleted. Once we apply the del function, the key value pairs for those keys get deleted.
Example
# Given dictionary ADict = {1: 'Mon', 2: 'Tue', 3: 'Wed',4:'Thu',5:'Fri'} # Get keys with value in 2,3. to_del = [key for key in ADict if key in(2,3)] # Delete keys for key in to_del: del ADict[key] # New Dictionary print(ADict)
Output
Running the above code gives us the following result −
{1: 'Mon', 4: 'Thu', 5: 'Fri'}
Using list with keys
We can create a list containing the keys from the dictionary and also use a conditional expression to select the keys to be used for deletion. In the below example we have only considered the keys with even values by comparing the remainder from division with two equal to zero.
Example
# Given dictionary ADict = {1: 'Mon', 2: 'Tue', 3: 'Wed',4:'Thu',5:'Fri'} # Get keys with even value for key in list(ADict): if (key%2) == 0: del ADict[key] # New Dictionary print(ADict)
Output
Running the above code gives us the following result −
{1: 'Mon', 3: 'Wed', 5: 'Fri'}
Using items to Delete
Instead of keys we can also use the items of the dictionary to delete the values. But after choosing the item we have to indirectly use the keys to select the items to be deleted.
Example
# Given dictionary ADict = {1: 'Mon', 2: 'Tue', 3: 'Wed',4:'Thu',5:'Fri'} NewDict = [] # Get keys with even value for key,val in ADict.items(): if val in('Tue','Fri'): NewDict.append(key) for i in NewDict: del ADict[i] # New Dictionary print(ADict)
Output
Running the above code gives us the following result −
{1: 'Mon', 3: 'Wed', 4: 'Thu'}
- Related Articles
- Python - Get items in sorted order from given dictionary
- Iterating through a dictionary in Swift
- Delete Dictionary Elements in Python
- Dictionary Methods in Python (cmp(), len(), items()…)
- C++ Remove an Entry Using Key from HashMap while Iterating Over It
- C++ Remove an Entry Using Value from HashMap while Iterating over It
- How to avoid ConcurrentModificationException while iterating a collection in java?
- Python Program to Multiply All the Items in a Dictionary
- Python program to find the sum of all items in a dictionary
- Delete and Edit items in Tkinter TreeView
- Count number of items in a dictionary value that is a list in Python
- How to access nested Python dictionary items via a list of keys?
- Get key from value in Dictionary in Python
- Python Extract specific keys from dictionary?
- Python – Create dictionary from the list
