{"id":6062,"date":"2023-10-29T10:40:24","date_gmt":"2023-10-29T05:10:24","guid":{"rendered":"https:\/\/python-programs.com\/?p=6062"},"modified":"2023-11-10T12:05:57","modified_gmt":"2023-11-10T06:35:57","slug":"python-iterate-over-dictionary-and-remove-items","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-iterate-over-dictionary-and-remove-items\/","title":{"rendered":"Python: Iterate over Dictionary and Remove items"},"content":{"rendered":"
Dictionary:<\/strong><\/p>\n 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.<\/p>\n Given a dictionary , the task is to iterate over the dictionary and remove items based on the given condition.<\/p>\n Examples:<\/strong><\/p>\n Input:<\/strong><\/p>\n Condition:<\/strong> Remove all the elements whose value is divisible by 3<\/p>\n Output:<\/strong><\/p>\n There are several ways to remove items from the dictionary some of them are:<\/p>\n 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.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 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.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n We may use dictionary comprehension to filter dictionary items based on condition and return the new dictionary to the original reference variable.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n 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.<\/p>\n 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 …<\/p>\ndictionary = {'hello': 89, 'this': 51, 'is': 100, 'BTechGeeks': 201}<\/pre>\n
{'hello': 89, 'is': 100}<\/pre>\n
Traverse the Dictionary and Remove items<\/h2>\n
\n
Method #1:Using del Keyword<\/h3>\n
# Given dictionary\r\ndictionary = {'hello': 89, 'this': 51, 'is': 100, 'BTechGeeks': 201}\r\n# Traverse the dictionary by making copy of it\r\nfor key, value in dict(dictionary).items():\r\n # if the value of key is divisible by 3 then remove it from original dictionary\r\n if(value % 3 == 0):\r\n # if the value is divisible by 3 the remove it from dictionary using del keyword\r\n del dictionary[key]\r\n# print the dictionary after removing all the elements which satisfies the condition\r\nprint(dictionary)\r\n<\/pre>\n
{'hello': 89, 'is': 100}<\/pre>\n
Method #2:Using pop()<\/h3>\n
# Given dictionary\r\ndictionary = {'hello': 89, 'this': 51, 'is': 100, 'BTechGeeks': 201}\r\n# Traverse the dictionary by making copy of it\r\nfor key, value in dict(dictionary).items():\r\n # if the value of key is divisible by 3 then remove it from original dictionary\r\n if(value % 3 == 0):\r\n # if the value is divisible by 3 the remove it from dictionary using pop()\r\n dictionary.pop(key)\r\n# print the dictionary after removing all the elements which satisfies the condition\r\nprint(dictionary)\r\n<\/pre>\n
{'hello': 89, 'is': 100}<\/pre>\n
Method #3:Using List Comprehension<\/h3>\n
# Given dictionary\r\ndictionary = {'hello': 89, 'this': 51, 'is': 100, 'BTechGeeks': 201}\r\n# using list comprehension\r\ndictionary = {key: value for key, value in dictionary.items()\r\n if value % 3 != 0}\r\n# print the dictionary after removing all the elements which satisfies the condition\r\nprint(dictionary)\r\n<\/pre>\n
{'hello': 89, 'is': 100}<\/pre>\n
\nRelated Programs<\/strong>:<\/p>\n\n