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


Groups > comp.lang.python > #52184

Re: Suggestion: PEP for popping slices from lists

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
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.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'pop': 0.05; 'element': 0.07; 'none,': 0.07; 'subject:PEP': 0.07; '[0,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; 'accepting': 0.14; 'element.': 0.16; 'popping': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'index': 0.16; 'wrote:': 0.18; 'things.': 0.19; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'extension': 0.26; 'this:': 0.26; 'header:X-Complaints-To:1': 0.27; 'list:': 0.30; 'subject:from': 0.34; 'something': 0.35; 'subject:lists': 0.35; 'but': 0.35; 'there': 0.35; 'doing': 0.36; 'similar': 0.36; 'list': 0.37; 'list.': 0.37; 'to:addr:python-list': 0.38; 'short': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'received:org': 0.40; 'even': 0.60; 'remove': 0.60; 'removing': 0.60; 'simple': 0.61; 'occur': 0.65; 'case?': 0.84; 'gabriel': 0.84; 'afford': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Suggestion: PEP for popping slices from lists
Date Thu, 08 Aug 2013 12:07:16 +0200
Organization None
References <dac4873d-4111-4880-9ce5-80f4ecf11685@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p508491dd.dip0.t-ipconnect.de
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.345.1375956533.1251.python-list@python.org> (permalink)
Lines 64
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1375956533 news.xs4all.nl 15909 [2001:888:2000:d::a6]:55366
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:52184

Show key headers only | View raw


Neatu Ovidiu Gabriel wrote:

> The list.pop(index) returns the element represented by the index and also
> reduces the list by removing that element. So it a short one liner for
> doing both things. But when it comes for popping a slice of the list there
> is nothing similar for doing in that simple way.
> 
> If you want to remove a slice and also reduce the list you will have
> something like this:
> 
> a_list, a_slice = a_list[:size], a_list[size:]
> 
> or even worser if you try to do the same for something in the middle.
> 
> My proposal is the extension of list.pop for accepting a way for popping
> slices.
> 
> When doing this:
> 
> a_list.pop(i,j)
> 
> pop will return the slice [i,j] and remove it from the list.
> 
> For popping from an index to the end:
> 
> a_list.pop(i, len(a_list))
> 
> Or even emptying the whole list:
> 
> a_list.pop(0, len(a_list))
> 
> 
> So this is it :)

You'd use 'del' to remove a slice from a list. So:

>>> def pop_slice(items, *indices):
...     x = slice(*indices)
...     result = items[x]
...     del items[x]
...     return result
... 
>>> items = range(10)
>>> pop_slice(items, 3)
[0, 1, 2]
>>> items
[3, 4, 5, 6, 7, 8, 9]
>>> pop_slice(items, 3, 4)
[6]
>>> items
[3, 4, 5, 7, 8, 9]
>>> pop_slice(items, None, None, 2)
[3, 5, 8]
>>> items
[4, 7, 9]

But what's your use case?
Does it occur often enough that you cannot afford a two-liner like

result = items[start:stop]
del items[start:stop]

?

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


Thread

Suggestion: PEP for popping slices from lists Neatu Ovidiu Gabriel <neatuovi@gmail.com> - 2013-08-08 02:45 -0700
  Re: Suggestion: PEP for popping slices from lists Peter Otten <__peter__@web.de> - 2013-08-08 12:07 +0200
    Re: Suggestion: PEP for popping slices from lists Neatu Ovidiu <neatuovi@gmail.com> - 2013-08-08 03:38 -0700
      Re: Suggestion: PEP for popping slices from lists Nicholas Cole <nicholas.cole@gmail.com> - 2013-08-08 12:12 +0100
        Re: Suggestion: PEP for popping slices from lists Neatu Ovidiu <neatuovi@gmail.com> - 2013-08-08 04:40 -0700
          Re: Suggestion: PEP for popping slices from lists Skip Montanaro <skip@pobox.com> - 2013-08-08 09:20 -0500
        Re: Suggestion: PEP for popping slices from lists Neatu Ovidiu <neatuovi@gmail.com> - 2013-08-08 04:44 -0700
          Re: Suggestion: PEP for popping slices from lists Neatu Ovidiu <neatuovi@gmail.com> - 2013-08-08 04:50 -0700
            Re: Suggestion: PEP for popping slices from lists Nicholas Cole <nicholas.cole@gmail.com> - 2013-08-08 14:08 +0100
              Re: Suggestion: PEP for popping slices from lists Neatu Ovidiu <neatuovi@gmail.com> - 2013-08-08 06:32 -0700
                Re: Suggestion: PEP for popping slices from lists Nicholas Cole <nicholas.cole@gmail.com> - 2013-08-08 15:03 +0100
          Re: Suggestion: PEP for popping slices from lists Terry Reedy <tjreedy@udel.edu> - 2013-08-08 16:03 -0400
          Re: Suggestion: PEP for popping slices from lists Joshua Landau <joshua@landau.ws> - 2013-08-08 22:32 +0100
          Re: Suggestion: PEP for popping slices from lists Tim Chase <python.list@tim.thechases.com> - 2013-08-08 16:50 -0500
          Re: Suggestion: PEP for popping slices from lists Terry Reedy <tjreedy@udel.edu> - 2013-08-08 18:10 -0400

csiph-web