You can use any()
on a generator-expression
:
any("noodles" in v for v in d.values())
which, in this case, gives:
True
why?
Well d.values()
returns an iterables
of all the values in the dictionary d
. So in this case, the result of d.values()
would be:
dict_values([['hello', 'hallo', 'salut', 'ciao', 'hola'], ['eggs', 'noodles', 'bacon']])
We then went to check if the string "noodles"
is in any of these values (lists
) .
This is really easy / readable to do. We just want to create a generator
that does exactly that - i.e. return a boolean
for each value indicating whether or not "noodles"
is in that list
.
Then, as we now have a generator
, we can use any()
which will just return a boolean
for the entire generator indicating whether or not there wer any Trues
when iterating through it.
So what this means in our case is if "noodles"
is in any of the values
.