breaking a list into smaller lists

Reinhold Birkenfeld reinhold-birkenfeld-nospam at wolke7.net
Thu Jun 17 09:17:28 CEST 2004


Maciej Dziardziel wrote:
> Bart Nessux wrote:
> 
>> 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
> 
> big = range(87)
> 
> print  [ big[i*10:i*10+10] for i in xrange(len(big)/10+int((len(big) %
> 10)>0) ) ]

I would add that you should definitely use the // operator in this case
as this code will break in 2.4.

Also, I would leave the modulo part out and write the thing as follows:

print [ big[i*10:(i+1)*10] for i in xrange((len(big)-1) // 10 + 1) ]

Reinhold

-- 
Wenn eine Linuxdistribution so wenig brauchbare Software wie Windows
mitbrächte, wäre das bedauerlich.  Was bei Windows der Umfang eines
"kompletten Betriebssystems" ist, nennt man bei Linux eine Rescuedisk.
  -- David Kastrup in de.comp.os.unix.linux.misc



More information about the Python-list mailing list