Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #108021
| From | justin walters <walters.justin01@gmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Best way to clean up list items? |
| Date | 2016-05-02 10:10 -0700 |
| 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> |
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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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