Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python check if string contains dictionary key

I have these strings:

>>> a="foo"
>>> b="foo_KEY"

And a dictionary like:

>>> DICT
{
'KEY': ['...'],
'KEY2': ['...', '...'],
...
}

I'd like to build a function that check if the test value is in any key of the input dict:

>>> has_key(a, DICT)
False
>>> has_key(b, DICT)
True

What is the most elegant way to do this task in Python 3?

like image 432
Rowandish Avatar asked Dec 30 '25 23:12

Rowandish


1 Answers

has_key = lambda a, d: any(k in a for k in d)
like image 103
Delgan Avatar answered Jan 02 '26 15:01

Delgan