Why does Python operator.itemgetter work given a comma separated list of numbers as indices, but not when the same list is packaged in a variable? -


here demonstration of issue i'm having. i'm new python, i'm pretty sure i'm overlooking obvious.

import operator  lista=["a","b","c","d","e","f"] print operator.itemgetter(1,3,5)(lista) >> ('b', 'd', 'f')  columns=1,3,5 print operator.itemgetter(columns)(lista) >> typeerror: list indices must integers, not tuple 

how around issue can use arbitrary list of indices specified argument @ program start?

this isn't itemgetter, function calling syntax. these different things:

operator.itemgetter(1, 3, 5)  operator.itemgetter((1, 3, 5)) 

the first 1 gets item #1, item #3, , item #5. second 1 gets item #(1, 3, 5). (which may make sense dictionary, not list.)

the second 1 when use itemgetter(columns). so, how first?

operator.itemgetter(*columns) 

that's it.

see unpacking argument lists in tutorial more information.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -