Python: Anyway to use map to get first element of a tuple
Use a list comprehension:
data = ((1,), (3,))
print([x[0] for x in data])
Use operator.itemgetter
:
from operator import itemgetter
map(itemgetter(0), ((1,), (3,)))
While the list comprehensions are generally more readable, itemgetter
is closest to what you asked for. It's also a bit faster:
>>> from timeit import timeit
>>> setup = 'from operator import itemgetter; lst=( ("a",), ("b",), (1,), (2,))'
>>> timeit('map(itemgetter(0), lst)', setup=setup)
0.13061050399846863
>>> timeit('[i[0] for i in lst]', setup=setup)
0.20302422800159547