Python - Remove Keys from dictionary starting with K

Python dictionaries are key-value data structures where keys are unique and immutable. Sometimes you need to remove keys that start with a specific character, such as 'K'. This article explores two common approaches to accomplish this task.

Syntax

Using del Statement

del dict_name[key]

The del statement removes a key-value pair from the dictionary. If the key doesn't exist, it raises a KeyError.

Using pop() Method

dict_name.pop(key)

The pop() method removes a key-value pair and returns the associated value. It also raises a KeyError if the key doesn't exist.

Algorithm

  • Step 1: Create or initialize the dictionary

  • Step 2: Identify keys that start with 'K'

  • Step 3: Remove the matching key-value pairs

  • Step 4: Display the updated dictionary

Using del Statement

This approach iterates through a copy of the dictionary keys and removes matching keys during iteration ?

# Create a dictionary
my_dict = {'K1': 1, 'K2': 2, 'K3': 3, 'A1': 4, 'A2': 5}
print("Original dictionary:", my_dict)

# Iterate through a copy of keys to avoid modification during iteration
for key in list(my_dict.keys()):
    # Check if the key starts with 'K'
    if key.startswith('K'):
        # Remove the key-value pair
        del my_dict[key]

# Print the updated dictionary
print("After removing keys starting with 'K':", my_dict)
Original dictionary: {'K1': 1, 'K2': 2, 'K3': 3, 'A1': 4, 'A2': 5}
After removing keys starting with 'K': {'A1': 4, 'A2': 5}

Using pop() Method

This approach first creates a list of keys to remove, then uses pop() to remove them safely ?

# Create a dictionary
my_dict = {'K1': 1, 'K2': 2, 'K3': 3, 'A1': 4, 'A2': 5}
print("Original dictionary:", my_dict)

# Create a list of keys that start with 'K'
keys_to_remove = [key for key in my_dict.keys() if key.startswith('K')]
print("Keys to remove:", keys_to_remove)

# Remove the keys using pop()
for key in keys_to_remove:
    my_dict.pop(key)

# Print the updated dictionary
print("After removing keys starting with 'K':", my_dict)
Original dictionary: {'K1': 1, 'K2': 2, 'K3': 3, 'A1': 4, 'A2': 5}
Keys to remove: ['K1', 'K2', 'K3']
After removing keys starting with 'K': {'A1': 4, 'A2': 5}

Using Dictionary Comprehension

A more Pythonic approach creates a new dictionary excluding unwanted keys ?

# Create a dictionary
my_dict = {'K1': 1, 'K2': 2, 'K3': 3, 'A1': 4, 'A2': 5}
print("Original dictionary:", my_dict)

# Create new dictionary without keys starting with 'K'
filtered_dict = {key: value for key, value in my_dict.items() if not key.startswith('K')}

print("Filtered dictionary:", filtered_dict)
Original dictionary: {'K1': 1, 'K2': 2, 'K3': 3, 'A1': 4, 'A2': 5}
Filtered dictionary: {'A1': 4, 'A2': 5}

Comparison

Method Modifies Original Best For
del statement Yes Direct key removal
pop() method Yes When you need removed values
Dictionary comprehension No (creates new) Functional programming style

Conclusion

Use del for simple key removal, pop() when you need the removed values, and dictionary comprehension for a functional approach. Always use list(dict.keys()) when modifying a dictionary during iteration.

Updated on: 2026-03-27T14:56:31+05:30

274 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements