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


Groups > comp.lang.python > #52225

Re: Suggestion: PEP for popping slices from lists

From Terry Reedy <tjreedy@udel.edu>
Subject Re: Suggestion: PEP for popping slices from lists
Date 2013-08-08 16:03 -0400
References <dac4873d-4111-4880-9ce5-80f4ecf11685@googlegroups.com> <mailman.345.1375956533.1251.python-list@python.org> <531d89a8-61e1-4117-b4cc-f02b3be30bb9@googlegroups.com> <mailman.347.1375960376.1251.python-list@python.org> <9bd6192b-2c71-4662-808a-fd7e74dedeb8@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.370.1375992220.1251.python-list@python.org> (permalink)

Show all headers | View raw


On 8/8/2013 7:44 AM, Neatu Ovidiu wrote:

Objection 1. People usually want to chunk sequences, not lists 
specifically. We now try to add new features that work with iterators 
and iterables generally, not just lists.

> This can be useful for doing all kinds of basic stuff. For example if you wanted to take 4 items of a list at at a time, do something with them and then update the list.
>
> jobs = ['job1', 'job2', 'job3', 'job4', 'job5', 'job6', 'job7', 'job8', 'job9', 'job10']
> while jobs:
>      print(jobs.pop_slice(0,4))
>
> should output
>
> 'job1', 'job2', 'job3', 'job4'
> 'job5', 'job6', 'job7', 'job8'
> 'job9', 'job10'

Objection 2. Usually when one wants to do this sort of thing, one wants 
the list either be intact or empty at the end. Emptying it chunk by 
chunk is worse than useless because it turns an O(n) process into an 
O(n*n) process.

The same is true of destructively iterating through a list with .pop(0). 
When I proposed the addition of .pop(), I meant it as the inverses of 
.append and did not include the (optional) index parameter. It is seldom 
used and usually only once, to remove a single leading item. The 
addition of iterators, which occurred *after* the addition of .pop, 
replaced some uses of .pop(0). For instance

first = mylist.pop(0)  # O(N) operation
for item in mylist:
   process(first, item)
del mylist

can, since 2.2, be written as

it = iter(mylist)
first = next(it)  #  O(1) operation
for item in it:
   process(first, item)
del mylist

If .pop were being added today, I would argue against including the 
index parameter.

-- 
Terry Jan Reedy

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