Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Newsgroups: comp.lang.python Subject: filter a list of strings Date: Thu, 3 Dec 2015 02:15:38 +0100 Lines: 20 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de pmAs6GNTaKYPzwo2M0LJwQPviuYQdxA2vA1KJeHKlvvQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.025 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'one?': 0.05; 'items)': 0.09; 'iterate': 0.09; 'long)': 0.09; 'received:172.16.0': 0.09; '10.000': 0.16; 'btw:': 0.16; 'item:': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'string': 0.17; 'code.': 0.23; "doesn't": 0.26; 'example': 0.26; 'subject:list': 0.26; 'correct': 0.28; 'list': 0.34; 'could': 0.35; 'done': 0.35; 'filter': 0.35; 'item': 0.35; 'there': 0.36; 'to:addr:python-list': 0.36; 'charset:us- ascii': 0.37; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'from:no real name:2**0': 0.60; 'header:Message-Id:1': 0.61; 'more': 0.63; 'course.': 0.67; '.....': 0.76; '100': 0.79; 'received:185': 0.91 X-Mailer: Claws Mail 3.9.3 (GTK+ 2.24.23; i686-pc-linux-gnu) X-Mailman-Approved-At: Thu, 03 Dec 2015 01:09:34 -0500 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:99930 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. btw: Is it correct to iterate over a copy (list[:]) of that string list and not the original one? -- GnuPGP-Key ID 0751A8EC