André
André

Reputation: 25554

How to transform list of tuples into a tuple?

How can I transform

[(212, u'comment', 0L), (205, u'main', 0L)]

into this?

(0L, 0L)

Upvotes: 0

Views: 95

Answers (1)

Andrew Clark
Andrew Clark

Reputation: 208435

>>> data = [(212, u'comment', 0L), (205, u'main', 0L)]
>>> tuple(x[2] for x in data)
(0L, 0L)

Or another method...

>>> zip(*data)   # just showing what zip(*data) does
[(212, 205), (u'comment', u'main'), (0L, 0L)]
>>> zip(*data)[2]
(0L, 0L)

Using itertools.imap() and operator.itemgetter()...

>>> from itertools import imap
>>> from operator import itemgetter
>>> tuple(imap(itemgetter(2), data))
(0L, 0L)

Upvotes: 4

Related Questions