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


Groups > comp.lang.python > #40188

Re: suggestions for improving code fragment please

From Terry Reedy <tjreedy@udel.edu>
Subject Re: suggestions for improving code fragment please
Date 2013-02-28 16:30 -0500
References <u8idnaoktKrcKbLMnZ2dnUVZ8uednZ2d@brightview.co.uk>
Newsgroups comp.lang.python
Message-ID <mailman.2686.1362087056.2939.python-list@python.org> (permalink)

Show all headers | View raw


On 2/28/2013 2:47 PM, The Night Tripper wrote:
> Hi there
>      I'm being very dumb ... how can I simplify this fragment?
>
>
>          if arglist:
>              arglist.pop(0)
>              if arglist:
>                  self.myparm1 = arglist.pop(0)
>                  if arglist:
>                      self.myparm2 = arglist.pop(0)
>                      if arglist:
>                          self.myparm3 = arglist.pop(0)
>                          if arglist:
>                              self.parm4 = arglist.pop(0)

To literally do the same thing

try:
     arglist.pop(0)
     self.myparm1 = arglist.pop(0)
     self.myparm2 = arglist.pop(0)
     self.myparm3 = arglist.pop(0)
     self.parm4 = arglist.pop(0)
except IndexError:
     pass

However, repeated popping from the front is O(n**2) instead of O(n).
Following should do the same, including removal from original arglist.

it = iter(arglist)
n = 0
try:
     next(it); n += 1
     self.myparm1 = next(it); n += 1
     self.myparm2 = next(it); n += 1
     self.myparm3 = next(it); n += 1
     self.parm4 = next(it); n += 1
except StopIteration:
     pass
arglist = arglist[n:]

-- 
Terry Jan Reedy

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


Thread

suggestions for improving code fragment please The Night Tripper <jkn+gg@nicorp.co.uk> - 2013-02-28 19:47 +0000
  Re: suggestions for improving code fragment please Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-28 11:55 -0800
  Re: suggestions for improving code fragment please Joel Goldstick <joel.goldstick@gmail.com> - 2013-02-28 14:56 -0500
    Re: suggestions for improving code fragment please The Night Tripper <jkn+gg@nicorp.co.uk> - 2013-02-28 23:20 +0000
  Re: suggestions for improving code fragment please Ian Kelly <ian.g.kelly@gmail.com> - 2013-02-28 12:58 -0700
  Re: suggestions for improving code fragment please Tim Chase <python.list@tim.thechases.com> - 2013-02-28 14:37 -0600
  Re: suggestions for improving code fragment please MRAB <python@mrabarnett.plus.com> - 2013-02-28 21:13 +0000
  Re: suggestions for improving code fragment please Mitya Sirenef <msirenef@lightbird.net> - 2013-02-28 16:22 -0500
  Re: suggestions for improving code fragment please Dave Angel <davea@davea.name> - 2013-02-28 16:28 -0500
  Re: suggestions for improving code fragment please Terry Reedy <tjreedy@udel.edu> - 2013-02-28 16:30 -0500
  Re: suggestions for improving code fragment please Tim Chase <python.list@tim.thechases.com> - 2013-02-28 16:44 -0600
  Re: suggestions for improving code fragment please Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-01 02:44 +0000

csiph-web