Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python - Find Keys with Specific Suffix in Dictionary
Finding keys with a specific suffix in a dictionary is a common task when filtering data based on key patterns. Python provides several approaches to accomplish this using built-in functions like endswith(), filter(), and comprehensions.
Let's consider this dictionary example
my_dict = {'compiler': 100, 'interpreter': 200, 'cooperative': 300, 'cloudbox': 400}
suffix = 'er'
print("Original dictionary:", my_dict)
print("Looking for keys ending with:", suffix)
Original dictionary: {'compiler': 100, 'interpreter': 200, 'cooperative': 300, 'cloudbox': 400}
Looking for keys ending with: er
Key Methods Used
endswith() Returns True if the string ends with the specified suffix
filter() Filters items based on a condition
lambda Creates anonymous functions for short operations
Using List Comprehension
List comprehension provides a concise way to filter keys that end with a specific suffix ?
def find_keys_with_suffix(dictionary, suffix):
return [key for key in dictionary if key.endswith(suffix)]
# Example dictionary
my_dict = {"dropbox": 1, "box": 2, "chat": 3, "mail": 4}
suffix = "ox"
# Find keys with suffix
result = find_keys_with_suffix(my_dict, suffix)
print(f"Keys with suffix '{suffix}': {result}")
Keys with suffix 'ox': ['dropbox', 'box']
Using filter() Function
The filter() function with lambda provides a functional programming approach ?
def find_keys_with_suffix(dictionary, suffix):
return list(filter(lambda key: key.endswith(suffix), dictionary))
# Example dictionary
my_dict = {"Evergreen": 1, "Beautiful": 2, "Fruitful": 3, "Golden": 4}
suffix = "ful"
# Find keys with suffix
result = find_keys_with_suffix(my_dict, suffix)
print(f"Keys with suffix '{suffix}': {result}")
Keys with suffix 'ful': ['Beautiful', 'Fruitful']
Using Loop and Conditional Statements
A traditional approach using loops offers more control over the filtering process ?
def find_keys_with_suffix(dictionary, suffix):
keys_with_suffix = []
for key in dictionary:
if key.endswith(suffix):
keys_with_suffix.append(key)
return keys_with_suffix
# Example dictionary
my_dict = {"apple": 1, "banana": 2, "cherry": 3, "orange": 4}
suffix = "y"
# Find keys with suffix
result = find_keys_with_suffix(my_dict, suffix)
print(f"Keys with suffix '{suffix}': {result}")
Keys with suffix 'y': ['cherry']
Using Dictionary Comprehension
Dictionary comprehension maintains both keys and values in the result ?
def find_items_with_suffix(dictionary, suffix):
return {key: value for key, value in dictionary.items() if key.endswith(suffix)}
# Example dictionary
my_dict = {"programs": 1, "algorithms": 2, "functions": 3, "variables": 4}
suffix = "ms"
# Find key-value pairs with suffix
result = find_items_with_suffix(my_dict, suffix)
print(f"Items with keys ending in '{suffix}': {result}")
Items with keys ending in 'ms': {'programs': 1, 'algorithms': 2}
Comparison
| Method | Returns | Best For |
|---|---|---|
| List Comprehension | Keys only | Simple, readable syntax |
| filter() + lambda | Keys only | Functional programming style |
| Loop with conditions | Keys only | Complex filtering logic |
| Dictionary Comprehension | Key-value pairs | Preserving dictionary structure |
Conclusion
Use list comprehension for simple key filtering with readable syntax. Choose dictionary comprehension when you need to preserve key-value pairs. The filter() method works well for functional programming approaches.
