Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #41984

Splitting a list into even size chunks in python?

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <nh.jones01@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.161
X-Spam-Level *
X-Spam-Evidence '*H*': 0.68; '*S*': 0.00; 'subject:into': 0.09; 'chunks.': 0.16; 'itertools': 0.16; 'length,': 0.16; 'subject:python': 0.16; 'obviously': 0.18; 'split': 0.19; 'help.': 0.21; 'print': 0.22; 'second': 0.26; 'wondering': 0.29; 'subject:list': 0.30; 'subject:size': 0.31; 'work:': 0.31; 'anyone': 0.31; 'lists': 0.32; 'received:209.85': 0.35; 'received:209.85.220': 0.35; 'something': 0.35; 'equal': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'there': 0.35; 'data,': 0.36; 'next': 0.36; 'useful': 0.36; 'subject:?': 0.36; 'hi,': 0.36; 'should': 0.36; 'two': 0.37; 'list': 0.37; 'received:209': 0.37; 'message-id:@gmail.com': 0.38; 'to:addr :python-list': 0.38; 'anything': 0.39; "couldn't": 0.39; 'extremely': 0.39; 'to:addr:python.org': 0.39; 'up,': 0.60; 'length': 0.61; 'first': 0.61; 'useful.': 0.68; 'obvious': 0.74; 'header:Reply-to:1': 0.77; 'reply-to:addr:gmail.com': 0.80; 'potentially': 0.81; 'header:Return-Path:2': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:date:to:from:reply-to:subject:message-id:x-priority :x-mailer:mime-version:content-transfer-encoding:content-type; bh=CMQLxCckp/mVYdfF3BPDGVMJ8bUYTEBWyZpJMUp5+pI=; b=TMmuZRrOP0maBDcnG26dXv4qMgfL7Hzv9H44US0RswyVnaQGAEOBqMNzTrY+T14JSH /2kMgaHN8eTDVwwFEu7ELoA9xk4rzn7ZrmbVuwsPCoe6lbNHOL79ItD/B+XekNi5xJjN oaOwmJYCLu7dBLqzzDL4HdSU3Hq1xU/aTLcihpsQckRznWJ54xSywOz/yZV4jRGp3PF5 grl/mM9UrbG6ge0LHkEig4ANoopgguQaBnG1ZK/jzlNur4XtV30hQ4lMfTweK+7WJRt3 NvMJccp48coYUsw9lWlpQw9XcXYKkB0hlGDjhvF9YnPcjLAH0r0b9zgPywnCzWio5pPY av+A==
X-Received by 10.66.235.41 with SMTP id uj9mr28604973pac.90.1364371566824; Wed, 27 Mar 2013 01:06:06 -0700 (PDT)
Date Wed, 27 Mar 2013 08:06:04 +0000
To python-list@python.org
From Norah Jones <nh.jones01@gmail.com>
Subject Splitting a list into even size chunks in python?
X-Priority 3
X-Mailer CatPHPMailer 5.1 (phpmailer.sourceforge.net)
MIME-Version 1.0
Content-Transfer-Encoding 8bit
Content-Type text/plain; charset="utf-8"
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
Reply-To Norah Jones <nh.jones01@gmail.com>
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3794.1364372003.2939.python-list@python.org> (permalink)
Lines 16
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1364372003 news.xs4all.nl 6967 [2001:888:2000:d::a6]:36680
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:41984

Show key headers only | View raw


Hi, 

I have a list of arbitrary length, and I need to split it up into equal size chunks. There are some obvious ways to do this, like keeping a counter and two lists, and when the second list fills up, add it to the first list and empty the second list for the next round of data, but this is potentially extremely expensive.

I was wondering if anyone had a good solution to this for lists of any length 

This should work:

    l = range(1, 1000)
    print chunks(l, 10) -> [ [ 1..10 ], [ 11..20 ], .., [ 991..999 ] ]

I was looking for something useful in itertools but I couldn't find anything obviously useful. 

Appretiate your help.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Splitting a list into even size chunks in python? Norah Jones <nh.jones01@gmail.com> - 2013-03-27 08:06 +0000

csiph-web