Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Mark Lawrence Newsgroups: comp.lang.python Subject: Re: filter a list of strings Date: Thu, 3 Dec 2015 12:28:07 +0000 Lines: 36 Message-ID: References: <3p9zlt5t5Vz5vN5@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 kscaqfFwEgJIFOY+P859kgmvUO2qKJ1s0rrMYcM2olLA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'from:addr:yahoo.co.uk': 0.05; 'one?': 0.05; 'items)': 0.09; 'iterate': 0.09; 'long)': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'targets': 0.09; '10.000': 0.16; 'btw:': 0.16; 'item:': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'targets:': 0.16; 'wrote:': 0.16; 'string': 0.17; 'language': 0.19; 'lawrence': 0.22; 'code.': 0.23; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; "doesn't": 0.26; 'example': 0.26; 'subject:list': 0.26; 'header:X-Complaints-To:1': 0.26; 'correct': 0.28; 'language.': 0.32; 'list': 0.34; 'could': 0.35; 'done': 0.35; 'filter': 0.35; 'item': 0.35; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'to:addr:python.org': 0.40; 'mark': 0.40; 'charset:windows-1252': 0.62; 'more': 0.63; 'our': 0.64; 'course.': 0.67; '.....': 0.76; '100': 0.79; 'pythonistas,': 0.84; 'absolutely': 0.88 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: 195.147.236.181 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.4.0 In-Reply-To: <3p9zlt5t5Vz5vN5@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:99955 On 03/12/2015 01:15, c.buhtz@posteo.jp wrote: > I would like to know how this could be done more elegant/pythonic. > > I have a big list (over 10.000 items) with strings (each 100 to 300 > chars long) and want to filter them. > > list = ..... > > for item in list[:]: > if 'Banana' in item: > list.remove(item) > if 'Car' in item: > list.remove(item) > > There are a lot of more conditions of course. This is just example code. > It doesn't look nice to me. To much redundance. targets = ['Banana', 'Car'...] for item in list[:]: for target in targets: if target in item: list.remove(item) > > btw: Is it correct to iterate over a copy (list[:]) of that string list > and not the original one? > Absolutely :) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence