Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: jmp Newsgroups: comp.lang.python Subject: Re: filter a list of strings Date: Thu, 03 Dec 2015 11:03:48 +0100 Lines: 43 Message-ID: References: <3pBBdn3nVcz5vP8@dovecot03.posteo.de> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de bzCnooddhuvoAtuF48+srgXWawxo+Sg+3eVr396Re9mw== 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; 'that?': 0.05; '[0,': 0.09; 'happen.': 0.09; 'part,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.10; 'python.': 0.11; 'applies': 0.15; '2*i': 0.16; 'clues': 0.16; 'element,': 0.16; 'mapping,': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'element': 0.18; 'odd': 0.18; 'assuming': 0.22; 'am,': 0.23; 'elements': 0.23; 'split': 0.23; 'header:In-Reply- To:1': 0.24; 'header:User-Agent:1': 0.26; 'example': 0.26; 'subject:list': 0.26; 'header:X-Complaints-To:1': 0.26; 'question': 0.27; 'function': 0.28; "i'm": 0.30; "i'd": 0.31; 'common': 0.33; 'combination': 0.33; 'know.': 0.34; 'list': 0.34; 'filter': 0.35; 'identity': 0.35; 'mapping': 0.35; 'but': 0.36; 'there': 0.36; '(and': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'received:org': 0.37; 'front': 0.38; 'google': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'your': 0.60; 'skip:u 10': 0.61; 'received:194': 0.61; 'back': 0.62; 'charset:windows-1252': 0.62; 'is.': 0.63; 'saw': 0.77; 'construct': 0.84; 'imagine': 0.96 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: paris.sequans.com User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 In-Reply-To: <3pBBdn3nVcz5vP8@dovecot03.posteo.de> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:99951 On 12/03/2015 10:27 AM, c.buhtz@posteo.jp wrote: > I often saw constructions like this > x for x in y if ... > But I don't understand that combination of the Python keywords (for, > in, if) I allready know. It is to complex to imagine what there really > happen. > > I understand this > for x in y: > if ... > > But what is about the 'x' in front of all that? > I'd advise you insist on understanding this construct as it is a very common (and useful) construct in python. It's a list comprehension, you can google it to get some clues about it. consider this example [2*i for i in [0,1,2,3,4] if i%2] == [2,6] you can split it in 3 parts: 1/ for i in [0,1,2,3,4] 2/ if i/2 3/ 2*i 1/ I'm assuming you understand this one 2/ this is the filter part 3/ this is the mapping part, it applies a function to each element To go back to your question "what is about the 'x' in front of all that". The x is the mapping part, but the function applied is the function identity which simply keeps the element as is. # map each element, no filter [2*i for i in [0,1,2,3,4]] == [0, 2, 4, 6, 8] # no mapping, keeping only odd elements [i for i in [0,1,2,3,4] if i%2] == [1,3] JM