We are given a dictionary we need to remove the Keys which are starting with K. For example we are give a dictionary d = {'Key1': 'value1', 'Key2': 'value2', 'other_Key': 'value3'} so that the output should be {'other_Key': 'value3'}
Using Dictionary Comprehension
Using dictionary comprehension we can filter out Key-value pairs where the Key starts with the letter "K" by checKing if not Key.startswith('K')
d = {'key1': 'value1', 'key2': 'value2', 'other_key': 'value3'}
# Dictionary comprehension to create a new dictionary that excludes keys starting with 'K'
d = {key: value for key, value in d.items() if not key.startswith('k')}
print(d)
Output
{'other_key': 'value3'}
Explanation:
- Dictionary comprehension iterates over the Key-value pairs and excludes those where the Key starts with the letter "K" using not Key.startswith('K').
- A new dictionary is created that only includes the Key-value pairs where the Key does not begin with "K"
Using del in a Loop
Code safely iterates over a list of keys using list(d.keys()). If a key starts with "K", it is removed from the dictionary using del.
d = {'Key1': 'value1', 'Key2': 'value2', 'other_key': 'value3'}
# Iterate over a list of the dictionary's keys (to avoid modifying the dictionary while iterating)
for key in list(d.keys()):
# Check if the key starts with the letter 'K'
if key.startswith('K'):
# If the key starts with 'K', remove it from the dictionary
del d[key]
print(d)
Output
{'other_key': 'value3'}
Explanation:
- Code creates a list of dictionary Keys (list(d.Keys())) to avoid modifying the dictionary while iterating over it.
- It checks if a Key starts with the letter "K" and deletes those Key-value pairs from the dictionary using del.
Using pop()
We can use pop() method to remove Keys that start with "K."
d = {'Key1': 'value1', 'Key2': 'value2', 'other_key': 'value3'}
# Iterate over a list of the dictionary's keys (to avoid modifying the dictionary while iterating)
for key in list(d.keys()):
# Check if the key starts with the letter 'K'
if key.startswith('K'):
# If the key starts with 'K', remove it from the dictionary using pop()
d.pop(key)
print(d)
Output
{'key1': 'value1', 'key2': 'value2', 'other_key': 'value3'}
Explanation:
- list(d.Keys()) creates a list of Keys to safely iterate through the dictionary while modifying it.
- Key.startswith('K') checKs if the current Key starts with letter "K".
- d.pop(Key) removes the Key-value pair from the dictionary using the pop() method.
Using dict.copy()
We can create a shallow copy of the dictionary and then remove the Keys from it.
d = {'Key1': 'value1', 'Key2': 'value2', 'other_key': 'value3'}
# Create a new dictionary excluding keys starting with 'k'
a = {key: value for key, value in d.items() if not key.startswith('K')}
print(a)
Output
{'other_key': 'value3'}
Explanation:
- Dictionary Comprehension It iterates over the key-value pairs in the original dictionary d and includes those where the key does not start with "K".
- Filtered Dictionary a new dictionary a is created excluding the key-value pairs with keys starting with the letter "K".