Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: filter a list of strings Date: Thu, 03 Dec 2015 11:13:57 +0100 Organization: None Lines: 53 Message-ID: References: <3pBBdn3nVcz5vP8@dovecot03.posteo.de> <201512030953.tB39rGLE011532@fido.openend.se> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de QOABHqViDHzTBKKLGRyxXgy5+NgJcso5ThdB6vc+Wchw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'reject': 0.05; 'that?': 0.05; 'executed': 0.07; 'variant': 0.07; 'creighton': 0.09; 'exits': 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; 'thu,': 0.15; '"blue': 0.16; '>>on': 0.16; 'item:': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'suggestion.': 0.16; 'wrote:': 0.16; 'laura': 0.18; 'skip:l 30': 0.18; '>>>': 0.20; '2015': 0.20; 'see:': 0.22; 'dec': 0.23; 'this:': 0.23; 'unlike': 0.23; 'header :User-Agent:1': 0.26; 'subject:list': 0.26; 'header:X-Complaints- To:1': 0.26; 'lot.': 0.29; 'preserve': 0.29; 'code': 0.30; '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; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'received:org': 0.37; 'front': 0.38; 'test': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'your': 0.60; 'saw': 0.77; 'url:tutorial': 0.91; 'imagine': 0.96 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd8cde.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 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:99952 Laura Creighton wrote: > 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 = [ 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-comprehensions > > But I would solve your problem like this: > > things_I_do_not_want = ['Car', 'Banana', ] > things_I_want = [] > > for item in list_of_everything_I_started_with: > if item not in things_I_do_not_want: > things_I_want.append(item) Note that unlike the original code your variant will not reject "Blue Banana". If the OP wants to preserve the '"Banana" in item' test he can use for item in list_of_everything_I_started_with: for unwanted in things_I_do_not_want: if unwanted in item: break else: # executed unless the for loop exits with break things_I_want.append(item) or things_I_want = [ item for item in list_of_everything_I_started_with if not any(unwanted in item for unwanted in things_I_do_not_want) ]