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 xrange(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?
At first, I designed it to split strings into substrings to parse string containing hex.
Today I turned it into complex, but still simple generator.
Arguments:
Obvious ones
iterable
is any iterable / iterator / generator containg / generating / iterating over input data,size
is, of course, size of chunk you want get,More interesting
reductor
is a callable, which receives generator iterating over content of chunk.I'd expect it to return sequence or string, but I don't demand that.
You can pass as this argument for example
list
,tuple
,set
,frozenset
,or anything fancier. I'd pass this function, returning string
(provided that
iterable
contains / generates / iterates over strings):Note that
reductor
can cause closing generator by raising exception.condition
is a callable which receives anything whatreductor
returned.It decides to approve & yield it (by returning anything evaluating to
True
),or to decline it & finish generator's work (by returning anything other or raising exception).
When number of elements in
iterable
is not divisible bysize
, whenit
gets exhausted,reductor
will receive generator generating less elements thansize
.Let's call these elements lasts elements.
I invited two functions to pass as this argument:
lambda x:x
- the lasts elements will be yielded.lambda x: len(x)==<size>
- the lasts elements will be rejected.replace
<size>
using number equal tosize
If you don't mind using an external package you could use
iteration_utilities.grouper
fromiteration_utilties
1. It supports all iterables (not just sequences):which prints:
In case the length isn't a multiple of the groupsize it also supports filling (the incomplete last group) or truncating (discarding the incomplete last group) the last one:
1 Disclaimer: I'm the author of that package.
If the lists are the same size, you can combine them into lists of 4-tuples with
zip()
. For example:Here's what the
zip()
function produces:If the lists are large, and you don't want to combine them into a bigger list, use
itertools.izip()
, which produces an iterator, rather than a list.One-liner, adhoc solution to iterate over a list
x
in chunks of size4
-In your second method, I would advance to the next group of 4 by doing this:
However, I haven't done any performance measurement so I don't know which one might be more efficient.
Having said that, I would usually choose the first method. It's not pretty, but that's often a consequence of interfacing with the outside world.