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

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically