Change language

Python | Matching a substring key in a dictionary

| |

Method # 1: Using items () + list comprehension
A combination of the above method can be used to accomplish this particular task, in which we simply turn key-value pairs using the items function, and a list comprehension helps with iteration and access logic.

# Python3 code to demonstrate how it works
Matching the substring key in the dictionary
# Using items () + list comprehension

 
# initializing dictionary

test_dict = { ’All’ : 1 , ’have’ : 2 , ’good’ : 3 , ’food’ : 4 }

 
# initialize the search key string

search_key = ’ood’

 
# print original dictionary

print ( "The original dictionary is:" + str (test_dict))

 
# Using items () + list comprehension
Matching the substring key in the dictionary

res = [val for key, val in test_dict.items () if search_key in key]

 
# print result

print ( "Values ​​for substring keys:" + str (res))

Output:

 The original dictionary is: {’All’: 1,’ food’: 4, ’have’: 2,’ good’: 3} Values ​​for substring keys: [4, 3] 

Method # 2: Using dict () + filter () + lambda
A combination of the above functions can be used to accomplish this specific task. In this case, the dict and filter functions are used to convert the result to a dictionary and query a substring in the list, respectively. The lambda performs the task of accessing all key-value pairs.

# Python3 code to demonstrate how it works
Matching a substring key in a dictionary
# Using dict () + filter () + lambda

 
# initializing dictionary

test_dict = { ’All’ : 1 , ’have’ : 2 , ’good’ : 3 , ’ food’ : 4 }

 
# initialize the search key string

search_key = ’ood’

  
# printing the original dictionary

print ( "The original dictionary is:" + str (test_dict))

 
# Using dict () + filter () + lambda
Matching the substring key in the dictionary

res = dict ( filter ( lambda item: search_key in item [ 0 ], test_dict.items ()))

  
# result print

print ( "Key-Value pair for substring keys: " + str (res))

Output:

 The original dictionary is: {’have’: 2,’ good’: 3, ’food’: 4,’ All’: 1} Key-Value pair for substring keys: {’good’: 3,’ food’: 4} 

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method