Functional way to compare things inside a list

Ivan@work ivan.cvetkovic at pakel.hr
Fri Sep 21 04:24:40 EDT 2012


On 21.09.2012 00:58, thorsopia at lavabit.com wrote:
> Hi,
>
> list = [{'1': []}, {'2': []}, {'3': ['4', '5']}]
>
> I want to check for a value (e.g. '4'), and get the key of the dictionary
> that contains that value.
> (Yep, this is bizarre.)
>
> some_magic(list, '4')
> => '3'
>
> What's the functional way to do it?
> Is it possible to do it with a one-liner?
>
>

Yes:

[key for d in list for key in d if '4' in d[key]]




More information about the Python-list mailing list