I have a list of tuples (always pairs) like this:
[(0, 1), (2, 3), (5, 7), (2, 1)]
I\'d like to find the sum of the first items in each pai
If you have a very large list or a generator that produces a large number of pairs you might want to use a generator based approach. For fun I use itemgetter() and imap(), too. A simple generator based approach might be enough, though.
import operator
import itertools
idx0 = operator.itemgetter(0)
list_of_pairs = [(0, 1), (2, 3), (5, 7), (2, 1)]
sum(itertools.imap(idx0, list_of_pairs))
Note that itertools.imap() is available in Python >= 2.3. So you can use a generator based approach there, too.