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


Groups > comp.lang.python > #52181 > unrolled thread

Suggestion: PEP for popping slices from lists

Started byNeatu Ovidiu Gabriel <neatuovi@gmail.com>
First post2013-08-08 02:45 -0700
Last post2013-08-08 18:10 -0400
Articles 15 — 8 participants

Back to article view | Back to comp.lang.python


Contents

  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

#52181 — Suggestion: PEP for popping slices from lists

FromNeatu Ovidiu Gabriel <neatuovi@gmail.com>
Date2013-08-08 02:45 -0700
SubjectSuggestion: PEP for popping slices from lists
Message-ID<dac4873d-4111-4880-9ce5-80f4ecf11685@googlegroups.com>
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 :)

[toc] | [next] | [standalone]


#52184

FromPeter Otten <__peter__@web.de>
Date2013-08-08 12:07 +0200
Message-ID<mailman.345.1375956533.1251.python-list@python.org>
In reply to#52181
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]

?

[toc] | [prev] | [next] | [standalone]


#52186

FromNeatu Ovidiu <neatuovi@gmail.com>
Date2013-08-08 03:38 -0700
Message-ID<531d89a8-61e1-4117-b4cc-f02b3be30bb9@googlegroups.com>
In reply to#52184
On Thursday, August 8, 2013 1:07:16 PM UTC+3, Peter Otten wrote:
> 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]
> 
> 
> 
> ?


> But what's your use case?
> 
> Does it occur often enough that you cannot afford a two-liner like
I think uses cases are plenty.
And how can I figure out how often it occurs? I don't have a hint, it remains an open question.

The issues I see so far with my proposal are:
  - what you said, how often it is used, if it deserves a place
  - maybe it should be a separate function like pop_slice to don't alter in anyway the behavior of good old pop
  - it will rise some backwards compatibility issues

I just find it's a more pythonic way to deal with this situation. One short line instead of two or one long line is better. This shortness of typing is the core feature of Python so I think my proposal it's leap towards simplicity.

[toc] | [prev] | [next] | [standalone]


#52188

FromNicholas Cole <nicholas.cole@gmail.com>
Date2013-08-08 12:12 +0100
Message-ID<mailman.347.1375960376.1251.python-list@python.org>
In reply to#52186

[Multipart message — attachments visible in raw view] — view raw

On Thu, Aug 8, 2013 at 11:38 AM, Neatu Ovidiu <neatuovi@gmail.com> wrote:

>
>
> > But what's your use case?
> >
> > Does it occur often enough that you cannot afford a two-liner like
> I think uses cases are plenty.
>
>
The possible cases I can think of would be better served with list
comprehensions (what you seem to want is to create lists based on other
lists) - but maybe I'm missing something.  Could we have one example?

N.

[toc] | [prev] | [next] | [standalone]


#52189

FromNeatu Ovidiu <neatuovi@gmail.com>
Date2013-08-08 04:40 -0700
Message-ID<efa20d58-0791-4375-a8aa-a9394b3ff51e@googlegroups.com>
In reply to#52188
On Thursday, August 8, 2013 2:12:53 PM UTC+3, Nicholas wrote:
> On Thu, Aug 8, 2013 at 11:38 AM, Neatu Ovidiu <neat...@gmail.com> wrote:
> 
> 
> 
> 
> 
> 
> 
> > But what's your use case?
> 
> >
> 
> > Does it occur often enough that you cannot afford a two-liner like
> 
> I think uses cases are plenty.
> 
> 
> 
> 
> The possible cases I can think of would be better served with list comprehensions (what you seem to want is to create lists based on other lists) - but maybe I'm missing something.  Could we have one example?
> 
> 
> 
> N.

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', 'job5', 'job6', 'job7', 'job8', 'job9', 'job10']
while jobs:
    print jobs.pop_slice(0,4)

[toc] | [prev] | [next] | [standalone]


#52201

FromSkip Montanaro <skip@pobox.com>
Date2013-08-08 09:20 -0500
Message-ID<mailman.353.1375973493.1251.python-list@python.org>
In reply to#52189
On Thu, Aug 8, 2013 at 6:40 AM, Neatu Ovidiu <neatuovi@gmail.com> wrote:
> 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', 'job5', 'job6', 'job7', 'job8', 'job9', 'job10']
> while jobs:
>     print jobs.pop_slice(0,4)

My initial reaction to this is that you want to infer structure where
none exists, so why not make the structure explicit?  In any case,
couldn't you subclass the list type and add a pop_slice method to do
what you want?  I'm not an iterator maven, but this also seems like
something you could mix up from something in the itertools module.

Skip

[toc] | [prev] | [next] | [standalone]


#52191

FromNeatu Ovidiu <neatuovi@gmail.com>
Date2013-08-08 04:44 -0700
Message-ID<9bd6192b-2c71-4662-808a-fd7e74dedeb8@googlegroups.com>
In reply to#52188
On Thursday, August 8, 2013 2:12:53 PM UTC+3, Nicholas wrote:
> On Thu, Aug 8, 2013 at 11:38 AM, Neatu Ovidiu <neat...@gmail.com> wrote:
> 
> 
> 
> 
> 
> 
> 
> > But what's your use case?
> 
> >
> 
> > Does it occur often enough that you cannot afford a two-liner like
> 
> I think uses cases are plenty.
> 
> 
> 
> 
> The possible cases I can think of would be better served with list comprehensions (what you seem to want is to create lists based on other lists) - but maybe I'm missing something.  Could we have one example?
> 
> 
> 
> N.

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'

[toc] | [prev] | [next] | [standalone]


#52192

FromNeatu Ovidiu <neatuovi@gmail.com>
Date2013-08-08 04:50 -0700
Message-ID<62769807-dff7-40a0-a060-d1e0b7c0685f@googlegroups.com>
In reply to#52191
On Thursday, August 8, 2013 2:44:05 PM UTC+3, Neatu Ovidiu wrote:
> On Thursday, August 8, 2013 2:12:53 PM UTC+3, Nicholas wrote:
> 
> > On Thu, Aug 8, 2013 at 11:38 AM, Neatu Ovidiu <neat...@gmail.com> wrote:
> 
> > 
> 
> > 
> 
> > 
> 
> > 
> 
> > 
> 
> > 
> 
> > 
> 
> > > But what's your use case?
> 
> > 
> 
> > >
> 
> > 
> 
> > > Does it occur often enough that you cannot afford a two-liner like
> 
> > 
> 
> > I think uses cases are plenty.
> 
> > 
> 
> > 
> 
> > 
> 
> > 
> 
> > The possible cases I can think of would be better served with list comprehensions (what you seem to want is to create lists based on other lists) - but maybe I'm missing something.  Could we have one example?
> 
> > 
> 
> > 
> 
> > 
> 
> > N.
> 
> 
> 
> 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'

The idea "popped" in my mind while thinking about this question.
http://stackoverflow.com/questions/18121416/right-split-a-string-into-groups-of-3/18122084
I founded the list comprehensions solutions kind of cumbersome and thought that there should be a simple way to do this kind of stuff.

[toc] | [prev] | [next] | [standalone]


#52194

FromNicholas Cole <nicholas.cole@gmail.com>
Date2013-08-08 14:08 +0100
Message-ID<mailman.350.1375967296.1251.python-list@python.org>
In reply to#52192

[Multipart message — attachments visible in raw view] — view raw

On Thu, Aug 8, 2013 at 12:50 PM, Neatu Ovidiu <neatuovi@gmail.com> wrote:

> On Thursday, August 8, 2013 2:44:05 PM UTC+3, Neatu Ovidiu wrote:
> > On Thursday, August 8, 2013 2:12:53 PM UTC+3, Nicholas wrote:
> >
> > > On Thu, Aug 8, 2013 at 11:38 AM, Neatu Ovidiu <neat...@gmail.com>
> wrote:
> >
> > >
> >
> > >
> >
> > >
> >
> > >
> >
> > >
> >
> > >
> >
> > >
> >
> > > > But what's your use case?
> >
> > >
> >
> > > >
> >
> > >
> >
> > > > Does it occur often enough that you cannot afford a two-liner like
> >
> > >
> >
> > > I think uses cases are plenty.
> >
> > >
> >
> > >
> >
> > >
> >
> > >
> >
> > > The possible cases I can think of would be better served with list
> comprehensions (what you seem to want is to create lists based on other
> lists) - but maybe I'm missing something.  Could we have one example?
> >
> > >
> >
> > >
> >
> > >
> >
> > > N.
> >
> >
> >
> > 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'
>
> The idea "popped" in my mind while thinking about this question.
>
> http://stackoverflow.com/questions/18121416/right-split-a-string-into-groups-of-3/18122084
> I founded the list comprehensions solutions kind of cumbersome and thought
> that there should be a simple way to do this kind of stuff.
> --
> http://mail.python.org/mailman/listinfo/python-list
>


Still seems a bit like a solution looking for a problem to me.

Why would you want to take four items at a time for a job from an arbitrary
part of a list?  I agree splitting a string into groups of three looks a
bit cumbersome in the example you've given, but a generator could be
written quite easily, and would almost certainly be quicker than trying to
alter the list in place.

Best wishes,

N.

[toc] | [prev] | [next] | [standalone]


#52196

FromNeatu Ovidiu <neatuovi@gmail.com>
Date2013-08-08 06:32 -0700
Message-ID<7c8c0342-f533-4e2e-9df4-71b2ccaf1929@googlegroups.com>
In reply to#52194
On Thursday, August 8, 2013 4:08:13 PM UTC+3, Nicholas wrote:
> On Thu, Aug 8, 2013 at 12:50 PM, Neatu Ovidiu <neat...@gmail.com> wrote:
> 
> 
> 
> 
> On Thursday, August 8, 2013 2:44:05 PM UTC+3, Neatu Ovidiu wrote:
> 
> > On Thursday, August 8, 2013 2:12:53 PM UTC+3, Nicholas wrote:
> 
> >
> 
> > > On Thu, Aug 8, 2013 at 11:38 AM, Neatu Ovidiu <neat...@gmail.com> wrote:
> 
> >
> 
> > >
> 
> >
> 
> > >
> 
> >
> 
> > >
> 
> >
> 
> > >
> 
> >
> 
> > >
> 
> >
> 
> > >
> 
> >
> 
> > >
> 
> >
> 
> > > > But what's your use case?
> 
> >
> 
> > >
> 
> >
> 
> > > >
> 
> >
> 
> > >
> 
> >
> 
> > > > Does it occur often enough that you cannot afford a two-liner like
> 
> >
> 
> > >
> 
> >
> 
> > > I think uses cases are plenty.
> 
> >
> 
> > >
> 
> >
> 
> > >
> 
> >
> 
> > >
> 
> >
> 
> > >
> 
> >
> 
> > > The possible cases I can think of would be better served with list comprehensions (what you seem to want is to create lists based on other lists) - but maybe I'm missing something.  Could we have one example?
> 
> 
> >
> 
> > >
> 
> >
> 
> > >
> 
> >
> 
> > >
> 
> >
> 
> > > N.
> 
> >
> 
> >
> 
> >
> 
> > 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'
> 
> 
> 
> The idea "popped" in my mind while thinking about this question.
> 
> http://stackoverflow.com/questions/18121416/right-split-a-string-into-groups-of-3/18122084
> 
> I founded the list comprehensions solutions kind of cumbersome and thought that there should be a simple way to do this kind of stuff.
> 
> --
> 
> http://mail.python.org/mailman/listinfo/python-list
> 
> 
> 
> 
> 
> Still seems a bit like a solution looking for a problem to me.
> 
> 
> 
> Why would you want to take four items at a time for a job from an arbitrary part of a list?  I agree splitting a string into groups of three looks a bit cumbersome in the example you've given, but a generator could be written quite easily, and would almost certainly be quicker than trying to alter the list in place.
> 
> 
> 
> Best wishes,
> 
> 
> N.

You are perfectly right. But I looked at it more like an improvement in the style of writing solutions and also a natural option because slices are highly present all over in python.

[toc] | [prev] | [next] | [standalone]


#52197

FromNicholas Cole <nicholas.cole@gmail.com>
Date2013-08-08 15:03 +0100
Message-ID<mailman.351.1375970608.1251.python-list@python.org>
In reply to#52196

[Multipart message — attachments visible in raw view] — view raw

On Thu, Aug 8, 2013 at 2:32 PM, Neatu Ovidiu <neatuovi@gmail.com> wrote:

> On Thursday, August 8, 2013 4:08:13 PM UTC+3, Nicholas wrote:
> > On Thu, Aug 8, 2013 at 12:50 PM, Neatu Ovidiu <neat...@gmail.com> wrote:
> >
> >
> >
> >
> > On Thursday, August 8, 2013 2:44:05 PM UTC+3, Neatu Ovidiu wrote:
> >
> > > On Thursday, August 8, 2013 2:12:53 PM UTC+3, Nicholas wrote:
> >
> > >
> >
> > > > On Thu, Aug 8, 2013 at 11:38 AM, Neatu Ovidiu <neat...@gmail.com>
> wrote:
> >
> > >
> >
> > > >
> >
> > >
> >
> > > >
> >
> > >
> >
> > > >
> >
> > >
> >
> > > >
> >
> > >
> >
> > > >
> >
> > >
> >
> > > >
> >
> > >
> >
> > > >
> >
> > >
> >
> > > > > But what's your use case?
> >
> > >
> >
> > > >
> >
> > >
> >
> > > > >
> >
> > >
> >
> > > >
> >
> > >
> >
> > > > > Does it occur often enough that you cannot afford a two-liner like
> >
> > >
> >
> > > >
> >
> > >
> >
> > > > I think uses cases are plenty.
> >
> > >
> >
> > > >
> >
> > >
> >
> > > >
> >
> > >
> >
> > > >
> >
> > >
> >
> > > >
> >
> > >
> >
> > > > The possible cases I can think of would be better served with list
> comprehensions (what you seem to want is to create lists based on other
> lists) - but maybe I'm missing something.  Could we have one example?
> >
> >
> > >
> >
> > > >
> >
> > >
> >
> > > >
> >
> > >
> >
> > > >
> >
> > >
> >
> > > > N.
> >
> > >
> >
> > >
> >
> > >
> >
> > > 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'
> >
> >
> >
> > The idea "popped" in my mind while thinking about this question.
> >
> >
> http://stackoverflow.com/questions/18121416/right-split-a-string-into-groups-of-3/18122084
> >
> > I founded the list comprehensions solutions kind of cumbersome and
> thought that there should be a simple way to do this kind of stuff.
> >
> > --
> >
> > http://mail.python.org/mailman/listinfo/python-list
> >
> >
> >
> >
> >
> > Still seems a bit like a solution looking for a problem to me.
> >
> >
> >
> > Why would you want to take four items at a time for a job from an
> arbitrary part of a list?  I agree splitting a string into groups of three
> looks a bit cumbersome in the example you've given, but a generator could
> be written quite easily, and would almost certainly be quicker than trying
> to alter the list in place.
> >
> >
> >
> > Best wishes,
> >
> >
> > N.
>
> You are perfectly right. But I looked at it more like an improvement in
> the style of writing solutions and also a natural option because slices are
> highly present all over in python.
>


I wasn't knocking it.  I was just trying to think it through.

[toc] | [prev] | [next] | [standalone]


#52225

FromTerry Reedy <tjreedy@udel.edu>
Date2013-08-08 16:03 -0400
Message-ID<mailman.370.1375992220.1251.python-list@python.org>
In reply to#52191
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

[toc] | [prev] | [next] | [standalone]


#52229

FromJoshua Landau <joshua@landau.ws>
Date2013-08-08 22:32 +0100
Message-ID<mailman.374.1375997589.1251.python-list@python.org>
In reply to#52191
On 8 August 2013 21:03, Terry Reedy <tjreedy@udel.edu> wrote:
> If .pop were being added today, I would argue against including the index
> parameter.

GASP! That's no fair!

1) When using pop you normally want to keep the mutability available,
so iter(mylist) is a no-go.
2) When using the index, it's often somewhere in the middle that
you're popping from
3) There's always deque for deques

That said you can always use blist which has O(log n) time complexity
for these pops. It's a hassle it's not stdlib :/.

[toc] | [prev] | [next] | [standalone]


#52230

FromTim Chase <python.list@tim.thechases.com>
Date2013-08-08 16:50 -0500
Message-ID<mailman.375.1375998548.1251.python-list@python.org>
In reply to#52191
On 2013-08-08 22:32, Joshua Landau wrote:
> On 8 August 2013 21:03, Terry Reedy <tjreedy@udel.edu> wrote:
> > If .pop were being added today, I would argue against including
> > the index parameter.
> 
> 3) There's always deque for deques

Unless you have pre-2.4 code, in which case I'm glad .pop() was
included (but in this hypothetical world, we'd have a deque in
pre-2.4 too :-D

-tkc


[toc] | [prev] | [next] | [standalone]


#52231

FromTerry Reedy <tjreedy@udel.edu>
Date2013-08-08 18:10 -0400
Message-ID<mailman.376.1375999869.1251.python-list@python.org>
In reply to#52191
On 8/8/2013 5:32 PM, Joshua Landau wrote:
> On 8 August 2013 21:03, Terry Reedy <tjreedy@udel.edu> wrote:
>> If .pop were being added today, I would argue against including the index
>> parameter.
>
> GASP! That's no fair!
>
> 1) When using pop you normally want to keep the mutability available,
> so iter(mylist) is a no-go.
> 2) When using the index, it's often somewhere in the middle that
> you're popping from

I have never done that and I do not believe I have ever seen that. It 
certainly is extremely rare in my experience. Removing an item *after* 
looking at it is something different.

for i in range(len(mylist), -1, -1):
   if pred(mylist[i]): del mylist[i]

-- 
Terry Jan Reedy

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web