Skip to content
Related Articles

Related Articles

Improve Article
Python | Delete items from dictionary while iterating
  • Last Updated : 21 Jan, 2021

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 
 

Python3




# 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 the following error: 
 

for key in myDict.keys():
RuntimeError: dictionary changed size during iteration

This runtime error says changing the size of the dictionary during iteration is not allowed(but it is possible). Now, let’s see all the different ways we can delete items from the dictionary while iterating.
 

Method #2: Using Dictionary comprehension
 



Python3




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

Python3




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

Python3




# 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', 3: 'Geeks'}

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. 
 

Python3




# 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'}

The above code will delete all key: value pairs, when val == ‘Geeks’. Two values are deleted since ‘Geeks’ appears twice in the above dictionary.
 

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :