A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/python-prefix-key-match-in-dictionary/ below:

Python | Prefix key match in dictionary

Python | Prefix key match in dictionary

Last Updated : 08 May, 2023

Sometimes, while working with dictionaries, we can have a problem in which we need to find the dictionary items that have some constraints on keys. One such constraint can be a prefix match on keys. Let's discuss certain ways in which this task can be performed. 

Method #1: Using dictionary comprehension + startswith() 

The combination of above two methods can be used to perform this particular task. In this, dictionary comprehension does the basic task of dictionary construction and startswith() performs the utility task of checking keys starting with specific prefix. 

Python3
# Python3 code to demonstrate working of
# Prefix key match in dictionary
# Using dictionary comprehension + startswith()

# Initialize dictionary
test_dict = {'tough': 1, 'to': 2, 'do': 3, 'todays': 4, 'work': 5}

# printing original dictionary
print("The original dictionary : " + str(test_dict))

# Initialize prefix
test_pref = 'to'

# Using dictionary comprehension + startswith()
# Prefix key match in dictionary
res = {key: val for key, val in test_dict.items()
       if key.startswith(test_pref)}

# printing result
print("Filtered dictionary keys are : " + str(res))

Output
The original dictionary : {'tough': 1, 'to': 2, 'do': 3, 'todays': 4, 'work': 5}
Filtered dictionary keys are : {'tough': 1, 'to': 2, 'todays': 4}

Time complexity: O(n), where n is the number of keys in the dictionary.
Auxiliary Space: O(m), where m is the number of keys in the filtered dictionary

Method #2 : Using map() + filter() + items() + startswith() This particular task can also be performed using the combination of above functions. The map function ties the filter logic of startswith() to each dictionary's items extracted by items() 

Python3
# Python3 code to demonstrate working of
# Prefix key match in dictionary
# Using map() + filter() + items() + startswith()

# Initialize dictionary
test_dict = {'tough': 1, 'to': 2, 'do': 3, 'todays': 4, 'work': 5}

# printing original dictionary
print("The original dictionary : " + str(test_dict))

# Initialize prefix
test_pref = 'to'

# Using map() + filter() + items() + startswith()
# Prefix key match in dictionary
res = dict(filter(lambda item: item[0].startswith(test_pref),
                  test_dict.items()))

# printing result
print("Filtered dictionary keys are : " + str(res))

Output
The original dictionary : {'tough': 1, 'to': 2, 'do': 3, 'todays': 4, 'work': 5}
Filtered dictionary keys are : {'tough': 1, 'to': 2, 'todays': 4}

Time complexity: O(n), where n is the number of items in the dictionary. This is because we are iterating over the items in the dictionary and applying the startswith function to each key, which takes O(m), where m is the length of the key.
Auxiliary Space: O(k), where k is the number of items that match the prefix, because the filtered dictionary contains only the items that match the prefix.

Method #3: Using find() method

Python3
# Python3 code to demonstrate working of
# Prefix key match in dictionary

# Initialize dictionary
test_dict = {'tough' : 1, 'to' : 2, 'do' : 3, 'todays' : 4, 'work' : 5}

# printing original dictionary
print("The original dictionary : " + str(test_dict))

# Initialize prefix
test_pref = 'to'

# Using dictionary comprehension + find()
# Prefix key match in dictionary
res = {key:val for key, val in test_dict.items()
                if key.find(test_pref)==0}

# printing result
print("Filtered dictionary keys are : " + str(res))

Output
The original dictionary : {'tough': 1, 'to': 2, 'do': 3, 'todays': 4, 'work': 5}
Filtered dictionary keys are : {'tough': 1, 'to': 2, 'todays': 4}

Time complexity: O(n) where n is the number of items in the dictionary. 
Auxiliary space: O(m) where m is the number of items in the filtered dictionary.

Method #4 : use re.match()

Python3
# Python3 code to demonstrate working of 
# Prefix key match in dictionary 
# Using re.match() 

# Importing re module 
import re 

# Initialize dictionary 
test_dict = {'tough': 1, 'to': 2, 'do': 3, 'todays': 4, 'work': 5} 

# printing original dictionary 
print("The original dictionary : " + str(test_dict)) 

# Initialize prefix 
test_pref = 'to' 

# Using re.match() 
# Prefix key match in dictionary 
res = {key: val for key, val in test_dict.items() 
        if re.match(test_pref, key)} 

# printing result 
print("Filtered dictionary keys are : " + str(res)) 
#This code is contributed by Edula Vinay Kumar Reddy

Output
The original dictionary : {'tough': 1, 'to': 2, 'do': 3, 'todays': 4, 'work': 5}
Filtered dictionary keys are : {'tough': 1, 'to': 2, 'todays': 4}

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #5 : Using slicing

Python3
# Python3 code to demonstrate working of
# Prefix key match in dictionary

# Initialize dictionary
test_dict = {'tough': 1, 'to': 2, 'do': 3, 'todays': 4, 'work': 5}

# printing original dictionary
print("The original dictionary : " + str(test_dict))

# Initialize prefix
test_pref = 'to'

res=dict()
# Prefix key match in dictionary
for i in list(test_dict.keys()):
    if(i[:len(test_pref)]==test_pref):
        res[i]=test_dict[i]
# printing result
print("Filtered dictionary keys are : " + str(res))

Output
The original dictionary : {'tough': 1, 'to': 2, 'do': 3, 'todays': 4, 'work': 5}
Filtered dictionary keys are : {'tough': 1, 'to': 2, 'todays': 4}

Time Complexity: O(n)
Auxiliary Space: O(n)

Method 6: Using a list comprehension 

Step-by-step approach

Python3
# Initialize dictionary
test_dict = {'tough': 1, 'to': 2, 'do': 3, 'todays': 4, 'work': 5}

# printing original dictionary
print("The original dictionary : " + str(test_dict))

# Initialize prefix
test_pref = 'to'

# Prefix key match in dictionary using list comprehension
filtered_keys = [key for key in test_dict.keys() if key.startswith(test_pref)]
filtered_dict = {key: test_dict[key] for key in filtered_keys}

# printing result
print("Filtered dictionary keys are : " + str(filtered_dict))

Output
The original dictionary : {'tough': 1, 'to': 2, 'do': 3, 'todays': 4, 'work': 5}
Filtered dictionary keys are : {'tough': 1, 'to': 2, 'todays': 4}

The time complexity of this method is O(n), where n is the number of keys in the dictionary.
The space complexity is O(k), where k is the number of keys that start with the given prefix.

Method #7: Using a for loop

Step-by-step explanation:

Python3
# Initialize dictionary
test_dict = {'tough': 1, 'to': 2, 'do': 3, 'todays': 4, 'work': 5}

# printing original dictionary
print("The original dictionary : " + str(test_dict))

# Initialize prefix
test_pref = 'to'

# Initialize empty dictionary
filtered_dict = {}

# Loop through dictionary keys and check if they start with prefix
for key in test_dict.keys():
    if key.startswith(test_pref):
        filtered_dict[key] = test_dict[key]

# printing result
print("Filtered dictionary keys are : " + str(filtered_dict))

Output
The original dictionary : {'tough': 1, 'to': 2, 'do': 3, 'todays': 4, 'work': 5}
Filtered dictionary keys are : {'tough': 1, 'to': 2, 'todays': 4}

Time complexity: O(n), where n is the number of keys in the dictionary.
Auxiliary space: O(k), where k is the number of keys that start with



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4