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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


Output:

{1: 'Geeks', 2: 'For'}

Another way:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


Output:

{1: 'Geeks', 2: 'For'}

Method #3: Using list(myDict)

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


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.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.