Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!fu-berlin.de!uni-berlin.de!news.dfncis.de!not-for-mail From: Helmut Jarausch Newsgroups: comp.lang.python Subject: Re: extracting a heapq in a for loop - there must be more elegant solution Date: 4 Dec 2013 09:37:08 GMT Lines: 27 Message-ID: References: <7124acb3-7ba2-4b4d-b6e6-8b49e00550fe@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: news.dfncis.de NOaWtdi+VWtk6CrrOEgefw5eIg4xACa9TCqnJZiDeYws9uFQa7A9WC/njf Cancel-Lock: sha1:gFRYQppsVm66dG4D4HXoksvMPm0= User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508 git://git.gnome.org/pan2) Xref: csiph.com comp.lang.python:60997 On Tue, 03 Dec 2013 04:40:26 -0800, rusi wrote: > On Tuesday, December 3, 2013 5:48:59 PM UTC+5:30, Helmut Jarausch wrote: >> Hi, >> >> I'd like to extracted elements from a heapq in a for loop. >> I feel my solution below is much too complicated. >> How to do it more elegantly? >> I know I could use a while loop but I don't like it. > > How about > > def in_sequence(h): > for i in range(len(h)): > yield heapq.heappop(h) > > Yeah its a bit fiddly: > 1. i needed for for but not used > 2. The range in the for loop -- normally a python 'smell' > > If python3 > > def ins3(h): > yield from (heapq.heappop(h) for i in range(len(h))) Many thanks, I'm using Python3 anyway! Helmut