How can I use itemgetter with a list variable instead of integers? For instance:
from operator import itemgetter
z = ['foo', 'bar','qux','zoo']
id = [1,3]
I have no problem with this:
In [5]: itemgetter(1,3)(z)
Out[5]: ('bar', 'zoo')
But this gave an error when I do this:
In [7]: itemgetter(id)(z)
TypeError Traceback (most recent call last)
<ipython-input-7-7ba47b19f282> in <module>()
TypeError: list indices must be integers, not list
How can I get itemgetter to correctly enter input from a list, i.e. using id?
source
share