Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'repeated': 0.07; 'subject:code': 0.07; 'try:': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'terry': 0.09; 'indexerror:': 0.16; 'popping': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'wrote:': 0.17; 'jan': 0.18; 'pass': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'subject:please': 0.27; 'header:X -Complaints-To:1': 0.28; "i'm": 0.29; 'to:addr:python-list': 0.33; 'front': 0.33; 'pm,': 0.35; 'there': 0.35; 'received:org': 0.36; 'except': 0.36; 'should': 0.36; 'being': 0.37; 'subject:: ': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'night': 0.62; 'removal': 0.65; 'dumb': 0.84; 'received:fios.verizon.net': 0.84; 'same,': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: suggestions for improving code fragment please Date: Thu, 28 Feb 2013 16:30:36 -0500 References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130215 Thunderbird/17.0.3 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 45 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1362087056 news.xs4all.nl 6969 [2001:888:2000:d::a6]:52714 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:40188 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