We are given a dictionary we need to remove Kth key from the dictionary. For example, we are given a dictionary d = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3', 'key4': 'value4'} we need to remove the key2 so that the output should be {'key1': 'value1', 'key3': 'value3', 'key4': 'value4'}.
Using del
del statement removes a specified key-value pair from a dictionary by directly referencing the key.
d = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3', 'key4': 'value4'}
K = 'key2'
# Using del
del d[K] # Removes 'key2' from the dictionary
print(f"After using del: {d}")
Output
After using del: {'key1': 'value1', 'key3': 'value3', 'key4': 'value4'}
Explanation:
delstatement is used to remove the key-value pair associated with the key'key2'from dictionarydwhich results in removal of that entry.- After using
del d[K]dictionarydis updated and key'key2'is no longer present in it.
Using pop()
pop() method can be used to remove the Kth key from a dictionary by first extracting the key at the desired index using list. Then pop() removes this key and returns its value ensuring safe deletion without modifying the dictionary during iteration.
d = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3', 'key4': 'value4'}
K = 'key2'
# Using pop()
rem_val = d.pop(K) # Removes 'key2' and returns its value
print(f"After using pop: {d}, Removed Value: {rem_val}")
Output
After using pop: {'key1': 'value1', 'key3': 'value3', 'key4': 'value4'}, Removed Value: value2
Explanation:
pop()method is used to remove the key-value pair for'key2'from dictionarydand return its value ('value2'), which is stored in variablerem_val.- After using
pop()dictionarydis updated with'key2'removed and removed value ('value2') is printed.
Using Dictionary Comprehension
Using dictionary comprehension we can create a new dictionary that excludes the key 'key2' by iterating over the original dictionary and filtering out the pair where key matches 'key2'.
d = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3', 'key4': 'value4'}
K='key1'
# Using dict comprehension
d = {key: value for key, value in d.items() if key != K} # Creates a new dictionary excluding 'key2'
print(f"After using dict comprehension: {d}")
Output
After using dict comprehension: {'key2': 'value2', 'key3': 'value3', 'key4': 'value4'}
Explanation:
- Dictionary comprehension iterates over the key-value pairs in the original dictionary and includes only those pairs where the key is not equal to
'key1'. - This results in a new dictionary that excludes the key
'key1'effectively removing it from original dictionary
Using filter()
Using the filter() function we can filter out the key-value pairs where the key matches 'key1' by applying a condition that checks if the key is not equal to 'key1'. This method returns an iterable which is then converted back into a dictionary effectively removing the key 'key1' from the original dictionary.
# Reset the dictionary for the next example
d = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3', 'key4': 'value4'}
K='key2'
# Using filter()
d = dict(filter(lambda item: item[0] != K, d.items())) # Filters out 'key2'
print(f"After using filter: {d}")
Output
After using filter: {'key1': 'value1', 'key3': 'value3', 'key4': 'value4'}
Explanation:
- filter() function is used with a lambda to exclude key-value pairs where the key is equal to 'key1' lambda function checks each dictionary item and returns True for keys that are not 'key1', effectively filtering them out.
- Filtered result is converted back into a dictionary using dict(), leaving the key 'key1' removed from the original dictionary.