De-tupleizing a list
Mark Niemczyk
prahamark at gmail.com
Tue Apr 26 15:39:32 EDT 2011
Some interesting performance comparisons, under Python 3.2. Times are relative, and are for an initial list of tuples with 500,000 items.
(1) ans = [] #relative time: 298
for item in lst:
ans += list(item)
return ans
(2) return [item[0] for item in lst] #relative time: 106
(3) from operator import itemgetter #relative time: 84
return list(map(itemgetter(0), lst))
(4) import itertools #relative time: 63
return list(itertools.chain.from_iterable(lst))
(5) return [x for (x,) in lst] #relative time: 52
With the caveat that 'your mileage may vary'
Regards,
Mark
More information about the Python-list
mailing list