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

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

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

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 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

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

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