Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Laura Creighton Newsgroups: comp.lang.python Subject: Re: filter a list of strings Date: Thu, 03 Dec 2015 10:53:16 +0100 Lines: 34 Message-ID: References: <3pBBdn3nVcz5vP8@dovecot03.posteo.de> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de y0wjn/1xfxAi39zE/oN7Zg4e0z4AZpg0hbLMSHv0bizQ== 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; 'cc:addr :python-list': 0.09; 'received:openend.se': 0.09; 'received:theraft.openend.se': 0.09; 'python': 0.10; 'thu,': 0.15; '>on': 0.16; 'cc:addr:lac': 0.16; 'cc:addr:openend.se': 0.16; 'from:addr:lac': 0.16; 'from:addr:openend.se': 0.16; 'from:name:laura creighton': 0.16; 'message-id:@fido.openend.se': 0.16; 'received:fido': 0.16; 'received:fido.openend.se': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'suggestion.': 0.16; 'wrote:': 0.16; 'laura': 0.18; 'skip:l 30': 0.18; '2015': 0.20; 'cc:addr:python.org': 0.20; 'cc:2**1': 0.22; 'see:': 0.22; 'dec': 0.23; 'this:': 0.23; 'subject:list': 0.26; 'lot.': 0.29; 'received:se': 0.29; 'cc:no real name:2**1': 0.29; 'problem': 0.33; 'url:python': 0.33; 'combination': 0.33; 'know.': 0.34; 'list': 0.34; 'item': 0.35; 'but': 0.36; 'there': 0.36; 'url:org': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'charset:us-ascii': 0.37; 'front': 0.38; 'your': 0.60; 'header:Message-Id:1': 0.61; 'saw': 0.77; 'header:In-reply-to:1': 0.84; 'url:tutorial': 0.91; 'imagine': 0.96 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openend.se; s=default; t=1449136402; bh=HDQk6WDyyi8HYVZr2fuNlOxRRdym5fSBGflaWxfCdgs=; h=To:cc:From:Subject:In-reply-to:References:Date:From; b=eqM0c0gpA/TM0N0FhuHp8kjoa3u4AclFB1CoczqduOijJzrO3gW30cb/xNM9wW9Fm +3nWkwMAJzH9++S5baAvIBIij/K785hDAR2TcYGYSFGaqJckDC48hdEHkTq2SgR+SX Ne9B3PXn37vH0FyHn3hov7EhP7Zt2GqQznEMy1OQ= In-reply-to: <3pBBdn3nVcz5vP8@dovecot03.posteo.de> Comments: In-reply-to message dated "Thu, 03 Dec 2015 10:27:19 +0100." Content-ID: <11530.1449136396.1@fido> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.3.9 (theraft.openend.se [82.96.5.2]); Thu, 03 Dec 2015 10:53:22 +0100 (CET) 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:99949 In a message of Thu, 03 Dec 2015 10:27:19 +0100, c.buhtz@posteo.jp writes: >Thank you for your suggestion. This will help a lot. > >On 2015-12-03 08:32 Jussi Piitulainen wrote: >> list =3D [ 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? This is a list comprehension. see: https://docs.python.org/2/tutorial/datastructures.html#list-comprehen= sions But I would solve your problem like this: things_I_do_not_want =3D ['Car', 'Banana', ] things_I_want =3D [] for item in list_of_everything_I_started_with: if item not in things_I_do_not_want: things_I_want.append(item) Laura