Python - Find Keys with Specific Suffix in Dictionary


The suffix of keys is a group of letters that is mentioned at the end of the word. In this problem statement, we need to create the specific variable that set the suffix key to filter the result by using some specific conditions. In Python, we have some built-in functions such as endswith(), filter(), lambda, append(), and, items() that will be used to Find Keys with specific suffix in Dictionary.

Let’s take an example of this.

The given dictionary,

my_dict = {'compiler': 100, 'interpreter': 200, 'cooperative': 300, 'cloudbox': 400, 'database': 500}

The suffix key is set to er,

Therefore, the final output becomes {'compiler': 100, 'interpreter': 20}

Syntax

The following syntax is used in the examples

endswith()

This is the predefined method used in Python and if the string ends with the given value it returns true, otherwise false.

filter()

The filter() method is applied when we need to filter the items based on specific conditions. In simple terms, it allows to iterate those elements that are extracted to satisfy the condition.

lambda

The function lambda offers a shortcut for declaring brief anonymous functions using the lambda keyword. The lambda function only has one expression.

append()

The append() is an in-built function in Python that inserts the element at the end of the list.

items()

This is a built-in method that can be used to return the view object. The object consists of a key with value pairs.

Using list Comprehension

In the following example, the program uses a list comprehension technique where variable key iterates through the input dictionary, and using an if-statement it set the condition for key with specific suffix with the help of built-in function endwith() to display the result.

Example

def key_suffix(dict, suffix):
    return [key for key in dict if key.endswith(suffix)]
# Create the dictionary
my_dict = {"dropbox": 1, "box": 2, "chat": 3, "mail": 4}
sfx = "ox"
# Calling function
res = key_suffix(my_dict, sfx)
# Display the result
print("Keys with suffix","[", sfx, "]", ":\n", res)

Output

Keys with suffix [ ox ] : 
['dropbox', 'box'] 

Using filter() Function

In the following example, the program uses a recursive function named key_suffix that accepts two parameters- dict and suffix to receive the input dictionary and suffix value. Then use some built-in functions- filter()[ filter the given suffix element for result], lambda[ to calculate the suffix operation by using built-in function endswith()], and at the end it will use the built-in function list() to get the suffix value in the form of a list to get the result.

Example

def key_suffix(dict, suffix):
    return list(filter(lambda key: key.endswith(suffix), dict))
# Create the list
my_dict = {"Evergreen": 1, "Beautiful": 2, "Fruitful": 3, "Golden": 4, "Brass": 5}
sfx = "ful"
# Calling function
res = key_suffix(my_dict, sfx)
# Display the display
print("Keys with suffix","[", sfx, "]", ":\n", res)

Output

 Keys with suffix [ ful ] :
 ['Beautiful', 'Fruitful']

Using loop and Conditional Statements

In the following example, the program uses the recursive function to set the empty list that stores the result in the form of a list during function return. Then using for loop it iterates the input dictionary of the program and using the if-statement it set the condition for keys with specific suffix by using the built-in function endswith() and append(). Next, it will implement function return to get the specific suffix as a result.

Example

def key_suffix(dict, suffix):
    keys_with_suffix = []
# Set the condition for operation and iteration of the specific suffix
    for key in dict:
        if key.endswith(suffix):
            keys_with_suffix.append(key)
    return keys_with_suffix
# Create the dictionary
my_dict = {"apple": 1, "banana": 2, "cherry": 3, "orange": 4, "Grapes": 5}
sfx = "y"
# Calling function
res = key_suffix(my_dict, sfx)
print("Keys with suffix","[", sfx, "]", ":\n", res)

Output

Keys with suffix [ y ] : 
['cherry'] 

Dictionary Comprehension

In the following example, we will use dictionary comprehension where the first parameter is set to key along with value pairs. Using items() it checks the view object to the dictionary and applies the if-statement to set the condition of key by using built-in function endswith() which specifies the keys with specific suffix and generate the result.

Example

def key_suffix(dict, suffix):
    return {key: value for key, value in dict.items() if key.endswith(suffix)}

# Create the dictionary
my_dict = {"Anonyms": 1, "Synonyms": 2, "Metaphor": 3, "Irony": 4}
sfx = "ms"
# Calling function
res = key_suffix(my_dict, sfx)
print("Keys with suffix","[", sfx, "]", ":\n", res)

Output

 Keys with suffix [ ms ] : 
{'Anonyms': 1, 'Synonyms': 2}

Conclusion

We know the specific keys with specific suffix in Dictionary are those keys that are represented in the form of string and specify the value in the form of an integer. There were used some methods such as dictionary comprehension, list comprehension, filter(), and, conditional statements to solve this problem statement. This type of program is generally used in various applications such as Data Analysis, Data Filtering, Logic building on Searching Algorithms, etc.

Updated on: 16-Aug-2023

140 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements