Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'algorithm': 0.03; 'result,': 0.05; '21,': 0.07; 'function,': 0.07; 'python': 0.09; 'lst': 0.09; 'sep': 0.09; 'def': 0.10; "wouldn't": 0.11; 'value.': 0.15; 'element.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'iterator,': 0.16; 'subject:compare': 0.16; 'wrote:': 0.17; 'australian': 0.17; 'variable': 0.20; 'written': 0.20; 'name;': 0.22; 'thus': 0.24; 'header:In-Reply-To:1': 0.25; '(e.g.': 0.27; 'am,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'subject:list': 0.28; 'dictionary': 0.29; 'probably': 0.29; "i'm": 0.29; 'fri,': 0.30; 'gets': 0.32; 'could': 0.32; 'to:addr:python- list': 0.33; 'hi,': 0.33; 'received:google.com': 0.34; 'list': 0.35; 'built-in': 0.35; 'filter': 0.35; 'so,': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'functional': 0.36; 'possible': 0.37; 'rather': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'sure': 0.38; 'to:addr:python.org': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'first': 0.61; 'grab': 0.64; 'here': 0.65; 'skip:n 30': 0.69; "'3'": 0.84; 'shadow': 0.91; 'proudly': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=PQsR1AADe0thrMPN/u6GTLY550CVkcQ6UBUxkNRQ9xA=; b=htEcypeQV3cAmchgjGjl3YCjnVFaTDYHVTLklm3QN9nsV/aQFICYK8OrXH5kdFxlC9 MIvXKcY7RutztLqP52dxPkexzyKGq0qCKfh3WQNg2F99oLwuEjKF70gzWU91zACW9A3t 1TUPu6CB6P2NIn9eDR58xzF/5dO6REJuCAwJwGj5+cn+GQLDlac0hXD8SuMMXy5p3uC8 oWR3ZqNI+LeINpDq/A1eiK+9ZKbIMRHSQ2YlPjRgSynRJiXtywvsCmFPQFaSqtPuGAt6 ZdXiHJTLKWPbyDiAL7WGzXlYqoGswYC4RFS+E6teHjx18UmPK1O/Jjmx1J8dkAUvuu7Z 7vmA== MIME-Version: 1.0 In-Reply-To: <63673.77.105.185.196.1348181901.squirrel@lavabit.com> References: <63673.77.105.185.196.1348181901.squirrel@lavabit.com> Date: Fri, 21 Sep 2012 18:23:34 +1000 Subject: Re: Functional way to compare things inside a list From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 38 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1348215817 news.xs4all.nl 6909 [2001:888:2000:d::a6]:48634 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:29606 On Fri, Sep 21, 2012 at 8:58 AM, 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? I'm thinking here of a list comprehension, filter(), and next() to grab the first element. Let's see... By the way, I wouldn't use 'list' as a variable name; you shadow the built-in type. lst = [{'1': []}, {'2': []}, {'3': ['4', '5']}] def find_n(n,dic): for key,searchme in dic.items(): if n in searchme: return key next(filter(None,[find_n('4',x) for x in lst])) That gets the result, but probably not in the cleanest way. I'm not sure off-hand if Python has a convenient way to curry a function, but if so, you could make the filter call rather simpler. Note that this is written for Python 3, where filter() returns an iterator, thus the algorithm is lazy and thus efficient (a very Australian way to do things). ChrisA Proudly Australian, proudly lazy!