Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #108021

Re: Best way to clean up list items?

Path csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From justin walters <walters.justin01@gmail.com>
Newsgroups comp.lang.python
Subject Re: Best way to clean up list items?
Date Mon, 2 May 2016 10:10:02 -0700
Lines 24
Message-ID <mailman.324.1462209011.32212.python-list@python.org> (permalink)
References <ng7v9d$ld8$1@dont-email.me> <lf5shy05rfg.fsf@ling.helsinki.fi> <CAO1D73FQqPwSpUxbQZx0_eFMGDWQvcVNfanbAv8JooRWMd3-yA@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de SS5vuQ5JQXVwAO9wESNb/gDf1ODNoo7aVWECKlOTeBfw==
Return-Path <walters.justin01@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.017
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'skip:[ 20': 0.03; "','": 0.16; '2016': 0.16; '\\r\\n': 0.16; 'dfs': 0.16; 'list1': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'have:': 0.18; '&gt;': 0.18; 'fine,': 0.22; 'item.': 0.22; 'am,': 0.23; 'wrote': 0.23; 'header:In-Reply-To:1': 0.24; 'subject:list': 0.26; 'message-id:@mail.gmail.com': 0.27; 'whitespace': 0.29; 'skip:[ 10': 0.31; 'compiled': 0.32; 'maybe': 0.33; '"")': 0.33; 'list': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'replace': 0.35; 'item': 0.35; 'but': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'skip:& 10': 0.37; 'received:209': 0.38; 'to:addr:python.org': 0.40; '2\xc2\xa0': 0.84; 'want:': 0.84; 'subject:Best': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to; bh=1OAJk5+8qgaRo67I7JfoLwpEn0f7pW3E+KjLUkePCkg=; b=dM1Xb6SCoW2iZ3fOt9YUitjRCScSedA6bAivoinKShHQ1VrkSndxLKNIlFQS4BHuIz LvKpLILlHovUf/dGJwXsEgZsAtPU/UAxhwWyf0lEanoezMGbpExJvzsU0cQkT1vx9l1N GExUw5dyBDkotChDUuzznN9TVqe9voouMaH8pexNx9FEg9uuQ4Q5q3Qku1/vxAvOeuu+ /3efhJ/xx5Jx23v/m/HnYUq8MTJJu90x+/+9diETHEwmvWqeRGoAQd1UsNHcwPz8LmhF dZf3R0WXHrf4At3/K17SboTKnEoU6kp4ETesKtOgyZr/MXofBMPKCMcg7WSoI+z3e3E4 V/BA==
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; bh=1OAJk5+8qgaRo67I7JfoLwpEn0f7pW3E+KjLUkePCkg=; b=EMVmq68i7ykpSenh3gjYI2PTYc7haCwVGK+pQzdp2YwZY66Y3znXuwRhD2M9ON5MFT BK6ZoT4auCo1GbWvrCl6fjG5UqKMAKcT7WXiA2t0RXvVyYMbzesjHjO78nTgfDyCbQl5 5DtBxub6dOph5GSDWR5qx/ko/or5Nyqpq5nFIME3fnANCa4Oww9JffIIoaEgeALoQkZM MaWEjVCSohgx7+dqp7jj8PSpZjauUU8oIlMNUT5h1Le9x3q4YG37qBkr50eaVoFWkEZ/ eOv9AiQ29GIq7Z0RNbABu7EurlvmO1MPOnYFCuO5cV1INMU+4MnIMBl0CLNKM30PJKp2 ZNMA==
X-Gm-Message-State AOPr4FXMz8EyIgGBLxpfIKnwJVe5j5vC1xt3+eM5PTYt3FCaQ7aIY3k5VON7fBY8qv8W7WGLYZyxXiZjriExrw==
X-Received by 10.112.172.199 with SMTP id be7mr15375295lbc.49.1462209002964; Mon, 02 May 2016 10:10:02 -0700 (PDT)
In-Reply-To <lf5shy05rfg.fsf@ling.helsinki.fi>
X-Content-Filtered-By Mailman/MimeDel 2.1.22
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.22
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID <CAO1D73FQqPwSpUxbQZx0_eFMGDWQvcVNfanbAv8JooRWMd3-yA@mail.gmail.com>
X-Mailman-Original-References <ng7v9d$ld8$1@dont-email.me> <lf5shy05rfg.fsf@ling.helsinki.fi>
Xref csiph.com comp.lang.python:108021

Show key headers only | View raw


On May 2, 2016 10:03 AM, "Jussi Piitulainen" <jussi.piitulainen@helsinki.fi>
wrote:
>
> DFS writes:
>
> > Have: list1 = ['\r\n   Item 1  ','  Item 2  ','\r\n  ']
> > Want: list1 = ['Item 1','Item 2']
> >
> >
> > I wrote this, which works fine, but maybe it can be tidier?
> >
> > 1. list2 = [t.replace("\r\n", "") for t in list1]   #remove \r\n
> > 2. list3 = [t.strip(' ') for t in list2]            #trim whitespace
> > 3. list1  = filter(None, list3)                     #remove empty items
> >
> > After each step:
> >
> > 1. list2 = ['   Item 1  ','  Item 2  ','  ']   #remove \r\n
> > 2. list3 = ['Item 1','Item 2','']              #trim whitespace
> > 3. list1 = ['Item 1','Item 2']                 #remove empty items

You could also try compiled regex to remove unwanted characters.

Then loop through the list and do a replace for each item.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Best way to clean up list items? DFS <nospam@dfs.com> - 2016-05-02 12:33 -0400
  Re: Best way to clean up list items? Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-05-02 19:57 +0300
    Re: Best way to clean up list items? justin walters <walters.justin01@gmail.com> - 2016-05-02 10:10 -0700
    Re: Best way to clean up list items? DFS <nospam@dfs.com> - 2016-05-02 14:06 -0400
      Re: Best way to clean up list items? Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-05-02 21:27 +0300
        Re: Best way to clean up list items? DFS <nospam@dfs.com> - 2016-05-02 15:04 -0400
  Re: Best way to clean up list items? Stephen Hansen <me+python@ixokai.io> - 2016-05-02 10:25 -0700
    Re: Best way to clean up list items? DFS <nospam@dfs.com> - 2016-05-02 14:09 -0400
      Re: Best way to clean up list items? Stephen Hansen <me+python@ixokai.io> - 2016-05-02 11:23 -0700
  Re: Best way to clean up list items? Peter Otten <__peter__@web.de> - 2016-05-02 19:30 +0200

csiph-web