Dictionary:
Dictionary is a mutable built-in Python Data Structure. It is conceptually similar to List, Set, and Tuples. It is, however, indexed by keys rather than a sequence of numbers and can be thought of as associative arrays. On a high level, it consists of a key and its associated value. The Dictionary class in Python describes a hash-table implementation.
Given a dictionary , the task is to iterate over the dictionary and remove items based on the given condition.
Examples:
Input:
Condition: Remove all the elements whose value is divisible by 3
dictionary = {'hello': 89, 'this': 51, 'is': 100, 'BTechGeeks': 201}
Output:
{'hello': 89, 'is': 100}
Traverse the Dictionary and Remove items
There are several ways to remove items from the dictionary some of them are:
Method #1:Using del Keyword
We may make a copy of the dictionary and iterate through it, deleting elements from the original dictionary as we go. For example, we have a string and integer dictionary. We want to iterate through this dictionary and delete items whose value is divisible by 3.
Below is the implementation:
# Given dictionary dictionary = {'hello': 89, 'this': 51, 'is': 100, 'BTechGeeks': 201} # Traverse the dictionary by making copy of it for key, value in dict(dictionary).items(): # if the value of key is divisible by 3 then remove it from original dictionary if(value % 3 == 0): # if the value is divisible by 3 the remove it from dictionary using del keyword del dictionary[key] # print the dictionary after removing all the elements which satisfies the condition print(dictionary)
Output:
{'hello': 89, 'is': 100}
Method #2:Using pop()
Similarly to the del keyword, we can make a copy of the original dictionary and iterate through it. We can add a condition to each pair during iteration, and if the condition is met, we can remove the element from the original dictionary. However, instead of using the del keyword, we will use the pop() function to delete elements during iteration.
Below is the implementation:
# Given dictionary dictionary = {'hello': 89, 'this': 51, 'is': 100, 'BTechGeeks': 201} # Traverse the dictionary by making copy of it for key, value in dict(dictionary).items(): # if the value of key is divisible by 3 then remove it from original dictionary if(value % 3 == 0): # if the value is divisible by 3 the remove it from dictionary using pop() dictionary.pop(key) # print the dictionary after removing all the elements which satisfies the condition print(dictionary)
Output:
{'hello': 89, 'is': 100}
Method #3:Using List Comprehension
We may use dictionary comprehension to filter dictionary items based on condition and return the new dictionary to the original reference variable.
Below is the implementation:
# Given dictionary dictionary = {'hello': 89, 'this': 51, 'is': 100, 'BTechGeeks': 201} # using list comprehension dictionary = {key: value for key, value in dictionary.items() if value % 3 != 0} # print the dictionary after removing all the elements which satisfies the condition print(dictionary)
Output:
{'hello': 89, 'is': 100}
Explanation:
We iterated over all key-value pairs in the dictionary and generated a new dictionary that only included objects whose values were not divisible by 3. The new dictionary was then assigned to the initial reference variable. It gave the impression that we had removed objects from the dictionary whose values were divisible by 3.
Related Programs:
- python iterate over dictionary all key value pairs
- python iterate over dictionary with index
- python tuple different ways to create a tuple and iterate over it
- python iterate over dictionary with list values
- python iterate loop over all nested dictionary values
- python how to iterate over nested dictionary dict of dicts
- loop iterate over all values of dictionary in python