Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unzip an iterator?

Given a list of pairs xys, the Python idiom to unzip it into two lists is:

xs, ys = zip(*xys)

If xys is an iterator, how can I unzip it into two iterators, without storing everything in memory?

like image 206
Grzegorz Chrupała Avatar asked Jun 12 '15 14:06

Grzegorz Chrupała


1 Answers

If you want to consume one iterator independently from the other, there's no way to avoid pulling stuff into memory, since one of the iterators will progress while the other does not (and hence has to buffer).

Something like this allows you to iterate over both the 'left items' and the 'right items' of the pairs:

 import itertools
 import operator

 it1, it2 = itertools.tee(xys)
 xs = map(operator.itemgetter(0), it1))
 ys = map(operator.itemgetter(1), it2))

 print(next(xs))
 print(next(ys))

...but keep in mind that if you consume only one iterator, the other will buffer items in memory until you start consuming them.

(Btw, assuming Python 3. In Python 2 you need to use itertools.imap(), not map().)

like image 67
wouter bolsterlee Avatar answered Oct 21 '22 02:10

wouter bolsterlee