breaking a list into smaller lists

Terry Reedy tjreedy at udel.edu
Thu Jun 17 03:57:47 CEST 2004


"Bart Nessux" <bart_nessux at hotmail.com> wrote in message
news:caqjke$mh8$1 at solaris.cc.vt.edu...
> I understand how a Python list can be broken into smaller lists by using
> slice like this:
>
> new_small_list_1 = BIG_LIST[0:1001] #0-1000

this is 1001 items: you probably want 0:1000

> new_small_list_2 = BIG_LIST[1001:2001] #1001-2000
> new_small_list_3 = BIG_LIST[2001:3001] #2001-3000

these are 1000 items, but with off-by-one correction, 1000:2000, etc

>
> However, I was wondering if there's an easier way to do this. For
> example, say I have a list that contains the names of 100,000 files that
> I wish to open and read. Because of file handle limits, I can only open
> roughly 1000 files at a time while the OS is doing other things.
>
> Is there a way to turn this big list into 100 small lists (each
> containing 1000 files) with one simple line of code?

using *your* pattern, corrected, but untested and written while slightly
tired:
[biglist[i:i+1000] for i in range(len(biglist)/1000)]

Terry J. Reedy







More information about the Python-list mailing list