Python - Remove Dictionary Key Words Last Updated : 27 Jan, 2025 Comments Improve Suggest changes Like Article Like Report We are given a dictionary we need to remove the dictionary key words. For example we are given a dictionary d = {'a': 1, 'b': 2} we need to remove the dictionary key words so that the output becomes {'b': 2} . We can use method like del , pop and other methods to do it.Using del keyworddel keyword is used to remove a specific key-value pair from a dictionary by specifying the key. If the key exists, it will be deleted; otherwise, it raises a KeyError. Python d = {'a': 1, 'b': 2} # Remove the key-value pair where the key is 'a' from the dictionary del d['a'] print(d) Output{'b': 2} Explanation:Dictionary d is initialized with two key-value pairs: 'a': 1 and 'b': 2.del keyword is used to remove the key-value pair with key 'a', and the modified dictionary {'b': 2} is printed.Using pop() methodpop() method removes and returns the value associated with a specified key in a dictionary. If the key is not found, it raises a KeyError unless a default value is provided. Python d = {'a': 1, 'b': 2} # Remove the key-value pair where the key is 'a' and return its value d.pop('a') print(d) Output{'b': 2} Explanation:pop('a') method removes key-value pair where the key is 'a' from the dictionary d and returns its value (1 in this case).Modified dictionary {'b': 2} is then printed as it no longer contains key 'a'Using popitem()popitem() method removes and returns the last key-value pair from a dictionary as a tuple. If the dictionary is empty, it raises a KeyError. Python d = {'a': 1, 'b': 2} # Removes the last key-value pair ('b': 2) from the dictionary d.popitem() print(d) Output{'a': 1} Explanation:popitem() method removes and returns the last key-value pair ('b': 2) from the dictionary d.modified dictionary {'a': 1} is printed, which now only contains the key 'a'Using Dictionary ComprehensionDictionary comprehension allows you to create a new dictionary by applying an expression to each key-value pair of an existing dictionary. Python d = {'a': 1, 'b': 2} # Use dictionary comprehension to create a new dictionary excluding the key 'a' d = {key: value for key, value in d.items() if key != 'a'} print(d) Output{'b': 2} Explanation:Dictionary comprehension iterates over the original dictionary d and creates a new dictionary, excluding the key 'a'.Modified dictionary, which now only contains the key 'b': 2, is printed. Comment More infoAdvertise with us Next Article Python - Remove Dictionary Key Words M manjeet_04 Follow Improve Article Tags : Python Python dictionary-programs Python string-programs Practice Tags : python Similar Reads Python - Ways to remove a key from dictionary We are given a dictionary and our task is to remove a specific key from it. For example, if we have the dictionary d = {"a": 1, "b": 2, "c": 3}, then after removing the key "b", the output will be {'a': 1, 'c': 3}.Using pop()pop() method removes a specific key from the dictionary and returns its cor 3 min read Remove Key from Dictionary List - Python We are given a list of dictionaries and our task is to remove a specific key from each dictionary in the list. For example, if we have the following list: li = [{'Gfg': 1, 'id': 2, 'best': 8}, {'Gfg': 4, 'id': 4, 'best': 10}, {'Gfg': 4, 'id': 8, 'best': 11}] and the key to remove is "id" then the re 3 min read Python - Remove item from dictionary when key is unknown We are given a dictionary we need to remove the item or the value of key which is unknown. For example, we are given a dictionary a = {'a': 10, 'b': 20, 'c': 30} we need to remove the key 'b' so that the output dictionary becomes like {'a': 10, 'c': 30} . To do this we can use various method and app 4 min read Python - Remove last element from dictionary Given a dictionary, the task is to remove the last occurring element, i.e. the last key-value pair in the dictionary. Example: Input: {1: 'Geeks', 2: 'For', 3: 'Geeks'} Output:{1: 'Geeks', 2: 'For'}Method 1: Using popitem() method Python dictionary popitem() method removes one key, value pair as a t 5 min read Python Dictionary keys() method keys() method in Python dictionary returns a view object that displays a list of all the keys in the dictionary. This view is dynamic, meaning it reflects any changes made to the dictionary (like adding or removing keys) after calling the method. Example:Pythond = {'A': 'Geeks', 'B': 'For', 'C': 'Ge 2 min read Like