Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '(1,': 0.09; '3),': 0.09; '5),': 0.09; 'behave': 0.09; 'res': 0.09; 'to:name:python-list': 0.15; '(2,': 0.16; '2),': 0.16; '3)]': 0.16; 'after,': 0.16; 'iterating': 0.16; 'possible?': 0.16; 'res:': 0.16; 'result:': 0.16; 'trying': 0.21; '(which': 0.26; 'message- id:@mail.gmail.com': 0.27; 'received:209.85.212': 0.28; 'noticed': 0.28; 'print': 0.32; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'list': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'should': 0.36; 'received:209': 0.37; 'to:addr:python.org': 0.39; 'first': 0.61; 'strange': 0.62 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:date:message-id:subject:from:to :content-type; bh=Kjyj3oOeer9rvvh0SCmcC2d9CrxOyHLt9BsMmqTFLaE=; b=u+Ae0GKTBqmK66TXcG9rMuDHQqp29hrskREuQdB/v8zPY3WUCroUuQGoqJeV4KsJp/ vj5QEPCMrLQsgZ527YBkXd2pUYojSh/KAkyUqRRJ8KoRsvCTbA/wigSC41Ll1TuhmGCt EJN+Iu86d6KyP1V+fUUBcRZnNuh4xOk05BBZzHxld8feWIoOwlk1UPIzxUkp2w9VyfyH oKSbgQhU1FfegUI05mIbiz65t+QtDDDKtqRmlonGF33UiinqYfi5tLj16Euxg2bkDRfR xJI/0ux5KXKY58KRUBjZrPNYv3dqCXsCvnXd7II3BMC58KAhPIVY2QFNSuqH/5beZH8c fuaA== MIME-Version: 1.0 X-Received: by 10.180.108.229 with SMTP id hn5mr20629947wib.28.1361896072859; Tue, 26 Feb 2013 08:27:52 -0800 (PST) Date: Tue, 26 Feb 2013 16:27:43 +0000 Subject: groupby behaviour From: andrea crotti To: python-list Content-Type: text/plain; charset=ISO-8859-1 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: 32 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1361896083 news.xs4all.nl 6872 [2001:888:2000:d::a6]:50189 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:39966 So I was trying to use groupby (which I used in the past), but I noticed a very strange thing if using list on the result: In [109]: s = sorted(s, key=keyfunc) In [110]: s Out[110]: [(1, 2), (1, 3), (1, 5), (2, 3)] In [117]: m2 = list(groupby(s, keyfunc)) In [118]: for name, res in m2: .....: for r in res: .....: print name, r .....: 2 (2, 3) In [119]: m2 = groupby(s, keyfunc) In [120]: for name, res in m2: for r in res: print name, r .....: 1 (1, 2) 1 (1, 3) 1 (1, 5) 2 (2, 3) How is that possible? Generating the list first should be the same as iterating after, how can it behave differently?