Python function that returns the value at index 0?
You need to use operator.itemgetter
>>> import operator
>>> pairs = [(0,1), (5,3)]
>>> xcoords = map(operator.itemgetter(0), pairs)
>>> xcoords
[0, 5]
In Python3, map
returns a map object, hence you need a list
call over it.
>>> list(map(operator.itemgetter(0), pairs))
[0, 5]
The most Pythonic approach would probably to use operator.itemgetter(0)
. It returns just such a function.
Another approach would be to call obj.__getitem__
directly. It's less Pythonic because it explicitly calls special method names, instead of allowing Python to infer what to call internally.