On 21.09.2012 00:58, thorso...@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?

simple, but possibly slow solution:

import itertools

def some_magic(list, search):
    return (key for key, val in itertools.chain(*(d.iteritems() for d in
list)) if search in val).next()

one-liner, yeah...
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to