Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.stack.nl!aioe.org!.POSTED!not-for-mail From: Mark H Harris Newsgroups: comp.lang.python Subject: Re: About python while statement and pop() Date: Thu, 12 Jun 2014 11:49:12 -0500 Organization: Aioe.org NNTP Server Lines: 33 Message-ID: References: <8d6207ab-b883-4940-8e53-75546a91d4dd@googlegroups.com> NNTP-Posting-Host: eSF12mcVRIwL+eMIMJ03mA.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 X-Notice: Filtered by postfilter v. 0.8.2 Xref: csiph.com comp.lang.python:73221 On 6/11/14 10:12 PM, hito koto wrote: > def foo(x): > y = [] > while x !=[]: > y.append(x.pop()) > return y > Consider this generator variation: >>> def poplist(L): done = False while done==False: yield L[::-1][:1:] L = L[::-1][1::][::-1] if len(L)==0: done=True >>> L1=[1, 2, 3, 4, 5, 6, 7] >>> for n in poplist(L1): print(n) [7] [6] [5] [4] [3] [2] [1] >>>