{"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

dictionary = {'hello': 89, 'this': 51, 'is': 100, 'BTechGeeks': 201}<\/pre>\n

Output:<\/strong><\/p>\n

{'hello': 89, 'is': 100}<\/pre>\n

Traverse the Dictionary and Remove items<\/h2>\n

There are several ways to remove items from the dictionary some of them are:<\/p>\n