Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Sivan Greenberg Newsgroups: comp.lang.python Subject: Re: filter a list of strings Date: Wed, 9 Dec 2015 16:35:23 +0200 Lines: 55 Message-ID: References: <3p9zlt5t5Vz5vN5@dovecot03.posteo.de> <2598115.t018ozAsXX@PointedEars.de> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de WEnrWu3eV99MTtYLmeL6bA+P6bDf6YzEQPAo8nN2pjzw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'one?': 0.05; 'cto': 0.07; 'cc:addr:python-list': 0.09; 'identifier': 0.09; 'items)': 0.09; 'iterate': 0.09; 'long)': 0.09; 'targets': 0.09; 'wed,': 0.15; '10.000': 0.16; 'btw:': 0.16; 'item:': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'targets:': 0.16; 'wrote:': 0.16; 'string': 0.17; '>': 0.18; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'lawrence': 0.22; 'variables.': 0.22; 'cc:no real name:2**0': 0.22; 'am,': 0.23; 'dec': 0.23; 'header :In-Reply-To:1': 0.24; 'subject:list': 0.26; 'message- id:@mail.gmail.com': 0.27; 'correct': 0.28; "skip:' 10": 0.28; 'selection': 0.29; 'twitter:': 0.30; 'url:mailman': 0.30; 'another': 0.32; 'url:python': 0.33; 'url:listinfo': 0.34; 'list': 0.34; 'requirements': 0.35; 'received:google.com': 0.35; 'could': 0.35; 'done': 0.35; 'filter': 0.35; 'item': 0.35; 'should': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; 'founder': 0.36; 'subject:: ': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'url:mail': 0.40; 'mark': 0.40; '&': 0.61; 'per': 0.62; 'thomas': 0.63; 'more': 0.63; 'e-mail.': 0.67; 'choose': 0.68; 'special': 0.73; '.....': 0.76; '100': 0.79; 'one\xe2\x80\x99s': 0.84; 'absolutely': 0.88 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vitakka-co.20150623.gappssmtp.com; s=20150623; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=RzRSKzXGc9dh+FcTxkXhVd7pIoPO5kN6zgCloAHd4L4=; b=QCZpsXpec0msWa5iIxSSZ3hA67gT/9SITr+TVL76UYbxG7UC/3ArzDhTaJaCLBY2Z6 W7yCRYP/YvCJzZ8xgnLfPcFVzgu0PSO+9eQEgcEcXLrywhCh4whUiTHwZjmktF9faEqY d7trEZeDPzyjs04iCxgqGafqwtfyk2xO2sdUUTN2CdbmbGorRX3eiuGDRWnmNvnfiwWO Q5aPqv5Xq0DsPwBy1uxiIq59V+P4cRgIaWi1HZ0NmfRWvlx8/dX6QEsMMJEAWWWdarzF V91DmpDKBy8T3hWa0io5pItUYPM36d6pgsxFUMKDYD8rNnDmKVdqwf0ofGsw+Y4OIz68 Dsvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=RzRSKzXGc9dh+FcTxkXhVd7pIoPO5kN6zgCloAHd4L4=; b=iNotyoBilcjkBguaUbNUslruVR36yVum5MgTweeCZwojkuWOZYyTFaavDcNVZZpSWR hIKqdpr+JBsDDnzFLP4AMZTA4NZ4TrvNAVGq+k+1ROGImZE+H9PmlKCA4IGkzPg+wF8g tSasIDfgYGCQvc2z9odp+HuT3i8K5e9q9hq4quRHfQJFkIuTwmOntFgvgQ1pMGVFL/pG NL4K45y6LFLRldommii4GxgfYYnssfLaND4N+GWzfHRmgyhWACXD7ulgyJJllothDQDs 5+ggcdnfMy8auNNsGRZKe1a1/nI+zCD4hgsdzM04+WECU/4bQQckT7xeKvBMl7UPKVps 470w== X-Gm-Message-State: ALoCoQlTWj6+uE0fjPPesWXjbd8c277a+ma2Ud8NSdhMDF1dXcTU5QHpdzx6mk6v6bIi0RQ9/cfTOZK09V96bzDV3Rw6HFDabw== X-Received: by 10.13.238.194 with SMTP id x185mr615063ywe.35.1449671724005; Wed, 09 Dec 2015 06:35:24 -0800 (PST) X-Originating-IP: [31.154.12.26] In-Reply-To: <2598115.t018ozAsXX@PointedEars.de> X-Mailman-Approved-At: Wed, 09 Dec 2015 09:37:19 -0500 X-Content-Filtered-By: Mailman/MimeDel 2.1.20+ 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:100199 That might also work: new_list =3D [i for i in the_list if i not in targets] # given you have no special requirements for the selection # out of 'targets' -Sivan On Wed, Dec 9, 2015 at 12:58 AM, Thomas 'PointedEars' Lahn < PointedEars@web.de> wrote: > Mark Lawrence wrote: > > > 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 =3D ..... > >> [=E2=80=A6] > > > > targets =3D ['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 lis= t > >> and not the original one? > > > > Absolutely :) > > However, =E2=80=9Clist=E2=80=9D is a built-in class/constructor that woul= d be overwritten > this way. One should choose another identifier than =E2=80=9Clist=E2=80= =9D for one=E2=80=99s > variables. > > -- > PointedEars > > Twitter: @PointedEars2 > Please do not cc me. / Bitte keine Kopien per E-Mail. > -- > https://mail.python.org/mailman/listinfo/python-list > --=20 Sivan Greenberg Co founder & CTO Vitakka Consulting