Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: suggestions for improving code fragment please

Reply
Thread Tools

Re: suggestions for improving code fragment please

 
 
Terry Reedy
Guest
Posts: n/a
 
      02-28-2013
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: suggestions for improving code fragment please MRAB Python 0 02-28-2013 09:13 PM
Re: suggestions for improving code fragment please Tim Chase Python 0 02-28-2013 08:37 PM
Re: suggestions for improving code fragment please Ian Kelly Python 0 02-28-2013 07:58 PM
Re: suggestions for improving code fragment please Joel Goldstick Python 0 02-28-2013 07:56 PM
Re: suggestions for improving code fragment please Rick Johnson Python 0 02-28-2013 07:55 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57