Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: filter a list of strings Date: Thu, 3 Dec 2015 20:40:51 +1100 Lines: 38 Message-ID: References: <3pBBdn3nVcz5vP8@dovecot03.posteo.de> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de Y2BvIYdZI/rtJP93H1z60QfkUESNUlSBy/OXqwCUX/Ig== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.012 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'that?': 0.05; 'cc:addr :python-list': 0.09; 'happen.': 0.09; 'python': 0.10; 'thu,': 0.15; '(before': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'suggestion.': 0.16; 'wrote:': 0.16; 'basically': 0.18; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'posted': 0.21; 'fairly': 0.22; 'bit': 0.23; 'dec': 0.23; 'this:': 0.23; 'header :In-Reply-To:1': 0.24; 'subject:list': 0.26; 'equivalent': 0.27; 'message-id:@mail.gmail.com': 0.27; 'lot.': 0.29; 'code': 0.30; 'call,': 0.33; 'combination': 0.33; 'know.': 0.34; 'list': 0.34; 'received:google.com': 0.35; 'list:': 0.35; 'item': 0.35; 'but': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'beginning': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'received:209.85.213': 0.37; 'front': 0.38; 'received:209': 0.38; 'thank': 0.38; 'goes': 0.39; 'well.': 0.40; 'called': 0.40; 'your': 0.60; 'saw': 0.77; 'chrisa': 0.84; 'post,': 0.84; 'to:none': 0.91; 'imagine': 0.96 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:cc :content-type; bh=hOpl0+MFb4kp3YRAPLExlW+8NucQsBsa9ABrGZPuduo=; b=CSdVWYz1hKee1hScKTJzsMSnnHL5st7bJG4G0V4Ehnu8I2aN1vTPWuWSRhyNwsLxz2 ka2ybXArTN+66nhb0ZS14R66UDsEXsjpJotIrdJPWVIE4X3d4Bj4WH2HAZk85S6eWiy0 uaTqbYYE+W6afGwxQLsNxySfRy4JCCQJfu6FpPt2eB9yacoBeyoIAK9NtLpuWXWqp0US 1daD65jPvG0vsAQEfxr80ih0tqlx1BzCi5foWcLNd+JaD0j6p6ZzOC3KZ+GKCC1CdTbz IrHRyC9ue7MEwGLCNcq1Hn8Piv3mCxgaGUe0gGJOQPAWn8HddUpQM3gGd7/JQiw+QSOv /5iw== X-Received: by 10.50.30.6 with SMTP id o6mr39107397igh.94.1449135651480; Thu, 03 Dec 2015 01:40:51 -0800 (PST) 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:99947 On Thu, Dec 3, 2015 at 8:27 PM, wrote: > Thank you for your suggestion. This will help a lot. > > On 2015-12-03 08:32 Jussi Piitulainen wrote: >> list = [ item for item in list >> if ( 'Banana' not in item and >> 'Car' not in item ) ] > > 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? It's called a *list comprehension*. The code Jussi posted is broadly equivalent to this: list = [] for item in list: if ( 'Banana' not in item and 'Car' not in item ): list.append(item) I recently came across this blog post, which visualizes comprehensions fairly well. http://treyhunner.com/2015/12/python-list-comprehensions-now-in-color/ The bit at the beginning (before the first 'for') goes inside a list.append(...) call, and then everything else is basically the same. ChrisA