Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #108023
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Peter Otten <__peter__@web.de> |
| Newsgroups | comp.lang.python |
| Subject | Re: Best way to clean up list items? |
| Date | Mon, 02 May 2016 19:30:41 +0200 |
| Organization | None |
| Lines | 50 |
| Message-ID | <mailman.326.1462210254.32212.python-list@python.org> (permalink) |
| References | <ng7v9d$ld8$1@dont-email.me> <ng82s2$9rg$1@ger.gmane.org> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Trace | news.uni-berlin.de gkPlZ7xY5FRBLv/zl2JxHwZLXNWzcs6KNLfFqw4DPsjQ== |
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'yet.': 0.03; 'items)': 0.09; 'iterate': 0.09; 'once;': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.10; "','": 0.16; '\\r\\n': 0.16; 'denote': 0.16; 'dfs': 0.16; 'iteration': 0.16; 'list1': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'stripped': 0.16; 'wrote:': 0.16; 'duplicate': 0.18; 'have:': 0.18; '>>>': 0.20; 'fine,': 0.22; 'wrote': 0.23; 'header:User-Agent:1': 0.26; 'subject:list': 0.26; 'header:X-Complaints-To:1': 0.26; 'whitespace': 0.29; 'code': 0.30; 'skip:[ 10': 0.31; 'maybe': 0.33; 'options': 0.33; '"")': 0.33; 'thanks!': 0.34; 'list': 0.34; 'item': 0.35; 'step': 0.36; 'but': 0.36; 'instead': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'received:org': 0.37; 'end': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'some': 0.40; "you'll": 0.61; 'here': 0.66; 'want:': 0.84; 'subject:Best': 0.93 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| X-Gmane-NNTP-Posting-Host | p57bd8fb7.dip0.t-ipconnect.de |
| User-Agent | KNode/4.13.3 |
| 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 | <ng82s2$9rg$1@ger.gmane.org> |
| X-Mailman-Original-References | <ng7v9d$ld8$1@dont-email.me> |
| Xref | csiph.com comp.lang.python:108023 |
Show key headers only | View raw
DFS wrote:
> 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
>
>
> Thanks!
s.strip() strips all whitespace, so you can combine steps 1 and 2:
>>> items = ['\r\n Item 1 ',' Item 2 ','\r\n ']
>>> stripped = (s.strip() for s in items)
The (...) instead of [...] denote a generator expression, so the iteration
has not started yet. The final step uses a list comprehension instead of
filter():
>>> [s for s in stripped if s]
['Item 1', 'Item 2']
That way the same code works with both Python 2 and Python 3. Note that you
can iterate over the generator expression only once; if you try it again
you'll end empty-handed:
>>> [s for s in stripped if s]
[]
If you want to do it in one step here are two options that both involve some
duplicate work:
>>> [s.strip() for s in items if s and not s.isspace()]
['Item 1', 'Item 2']
>>> [s.strip() for s in items if s.strip()]
['Item 1', 'Item 2']
Back to comp.lang.python | Previous | Next — Previous 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