Python | Delete items from dictionary while iterating
Dictionary in Python is an unordered collection of data values, used to store data values like a map, which unlike other Data Types that hold only single value as an element, Dictionary holds key:value pair. 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.
Let’s see how to delete items from a dictionary while iterating over it.
Method #1: Using del() method
# Creating a dictionary myDict = {1: 'Geeks', 2: 'For', 3: 'Geeks'} # Iterating through the keys for key in myDict.keys(): if key == 2: del myDict[key] # Modified Dictionary print(myDict) |
Output:
{1: 'Geeks', 3: 'Geeks'}
The above code works fine for Python2, but when we run it with Python3, it throws following eoor:
for key in myDict.keys(): RuntimeError: dictionary changed size during iteration
This runtime error says changing the size of dictionary during iteration is not allowed(but it is possible). Now, let’s see all the different ways we can delete items from dictionary while iterating.
Method #2: Using Dictionary comprehension
# Creating a dictionary myDict = {1: 'Geeks', 2: 'For', 3: 'Geeks'} # Using dictionary comprehension to find list # keys having value in 3. delete = [key for key in myDict if key == 3] # delete the key for key in delete: del myDict[key] # Modified Dictionary print(myDict) |
Output:
{1: 'Geeks', 2: 'For'}
Another way:
# Creating a dictionary myDict = {1: 'Geeks', 2: 'For', 3: 'Geeks'} # Using dictionary comprehension for key in [key for key in myDict if key == 3]: del myDict[key] # Modified Dictionary print(myDict) |
Output:
{1: 'Geeks', 2: 'For'}
Method #3: Using list(myDict)
# Creating a dictionary myDict = {1: 'Geeks', 2: 'For', 3: 'Geeks'} # Iterating through the list of keys for key in list(myDict): if key == 2: del myDict[key] # Modified Dictionary print(myDict) |
Output:
{1: 'Geeks', 2: 'For'}
Method #4: Using a list of keys
Create a list delete and add keys of all the values we want to delete. Iterate through each key in that list and keep deleting them.
# Creating a dictionary myDict = {1: 'Geeks', 2: 'For', 3: 'Geeks'} # list of delete keys delete = [] for key, val in myDict.items(): if val == 'Geeks': delete.append(key) for i in delete: del myDict[i] # Modified Dictionary print(myDict) |
Output:
{2: 'For'}
Above code will delete all key:value pairs, when val == ‘Geeks’. Two values are deleted, since ‘Geeks’ appears twice in above dictionary.
Recommended Posts:
- Python | Get first K items in dictionary
- Dictionary Methods in Python | Set 1 (cmp(), len(), items()...)
- Python Dictionary | items() method
- Python | Sort the items alphabetically from given dictionary
- Python | Get items in sorted order from given dictionary
- Python program to find the sum of all items in a dictionary
- Python | Type conversion of dictionary items
- Python | Count number of items in a dictionary value that is a list
- Python | Convert nested dictionary into flattened dictionary
- Python | Convert flattened dictionary into nested dictionary
- Python | Convert string dictionary to dictionary
- Python | Remove items from Set
- Python | Pandas Series.items()
- Python | Associating a single value with all list items
- Python | Convert an array to an ordinary list with the same items
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



