breaking a list into smaller lists

Phil Frost indigo at bitglue.com
Thu Jun 17 03:31:15 CEST 2004


One could easilly break it into a list of small lists:

small = [ BIG_LIST[i:i+1000] for i in xrange(0,len(BIG_LIST),1000) ]

On Wed, Jun 16, 2004 at 07:04:10PM -0400, Bart Nessux wrote:
> 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
> new_small_list_2 = BIG_LIST[1001:2001]		#1001-2000
> new_small_list_3 = BIG_LIST[2001:3001]		#2001-3000
> ...
> ...
> ...
> 
> 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?
> 
> Thanks,
> Bart




More information about the Python-list mailing list