Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the most "pythonic" way to iterate over a list in chunks?

I have a Python script which takes as input a list of integers, which I need to work with four integers at a time. Unfortunately, I don't have control of the input, or I'd have it passed in as a list of four-element tuples. Currently, I'm iterating over it this way:

for i in range(0, len(ints), 4):     # dummy op for example code     foo += ints[i] * ints[i + 1] + ints[i + 2] * ints[i + 3] 

It looks a lot like "C-think", though, which makes me suspect there's a more pythonic way of dealing with this situation. The list is discarded after iterating, so it needn't be preserved. Perhaps something like this would be better?

while ints:     foo += ints[0] * ints[1] + ints[2] * ints[3]     ints[0:4] = [] 

Still doesn't quite "feel" right, though. :-/

Related question: How do you split a list into evenly sized chunks in Python?

like image 671
Ben Blank Avatar asked Jan 12 '09 02:01

Ben Blank


People also ask

What is the best way to iterate a list?

The best way to iterate the list in terms of performance would be to use iterators ( your second approach using foreach ).

Is it faster to iterate through list or set Python?

Iterating over a List is much much faster than iterating over a set. The currently accepted answer is using a very small set and list and hence, the difference is negligible there.

Are for loops Pythonic?

💡 Python's “for” loops are “for-each” loops I would consider this solution to be quite Pythonic. It's nice and clean and almost reads like pseudo code from a text book. I don't have to keep track of the container's size or a running index to access elements.


1 Answers

def chunker(seq, size):     return (seq[pos:pos + size] for pos in range(0, len(seq), size)) # (in python 2 use xrange() instead of range() to avoid allocating a list) 

Works with any sequence:

text = "I am a very, very helpful text"  for group in chunker(text, 7):    print(repr(group),) # 'I am a ' 'very, v' 'ery hel' 'pful te' 'xt'  print '|'.join(chunker(text, 10)) # I am a ver|y, very he|lpful text  animals = ['cat', 'dog', 'rabbit', 'duck', 'bird', 'cow', 'gnu', 'fish']  for group in chunker(animals, 3):     print(group) # ['cat', 'dog', 'rabbit'] # ['duck', 'bird', 'cow'] # ['gnu', 'fish'] 
like image 152
nosklo Avatar answered Oct 12 '22 13:10

nosklo