Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'syntax': 0.04; 'true,': 0.05; 'subject:two': 0.07; '[],': 0.09; 'iterate': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:into': 0.09; 'expression.': 0.16; 'iterator': 0.16; 'lambda': 0.16; 'path:': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'smith"': 0.16; 'subject:based': 0.16; ':-)': 0.16; 'wrote:': 0.18; 'memory': 0.22; 'header:User- Agent:1': 0.23; 'rid': 0.24; 'pass': 0.26; 'least': 0.26; 'gets': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'subject:list': 0.30; "i'm": 0.30; 'constant': 0.31; 'continuing': 0.33; 'could': 0.34; 'requirement': 0.35; 'but': 0.35; 'subject:?': 0.36; 'list': 0.37; 'skip:o 20': 0.38; '8bit%:86': 0.38; 'to:addr:python-list': 0.38; 'anything': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'phil': 0.84; 'usage.': 0.84; '2013': 0.98 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Serhiy Storchaka Subject: Re: Split a list into two parts based on a filter? Date: Wed, 12 Jun 2013 19:28:45 +0300 References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: 78.111.187.215 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/20130510 Thunderbird/17.0.6 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: 21 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1371054538 news.xs4all.nl 15960 [2001:888:2000:d::a6]:37732 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:47816 12.06.13 09:32, Phil Connell написав(ла): > On 12 Jun 2013 01:36, "Roy Smith" > > wrote: > > Well, continuing down this somewhat bizarre path: > > > > new_songs, old_songs = [], [] > > itertools.takewhile( > > lambda x: True, > > (new_songs if s.is_new() else old_songs).append(s) for s in songs) > > ) > > > > I'm not sure I got the syntax exactly right, but the idea is anything > > that will iterate over a generator expression. That at least gets rid > > of the memory requirement to hold the throw-away list :-) > > You could equivalently pass the generator to deque() with maxlen=0 - > this consumes the iterator with constant memory usage. any((new_songs if s.is_new() else old_songs).append(s) for s in songs)