How can I make itemgetter to take input from list variable?
Question: How can I use itemgetter with list variable instead of integers? For example:

    
    
    from operator import itemgetter
    z = ['foo', 'bar','qux','zoo']
    id = [1,3]
    

I have no problem doing this:

    
    
    In [5]: itemgetter(1,3)(z)
    Out[5]: ('bar', 'zoo')
    

But it gave error when I do this:

    
    
    In [7]: itemgetter(id)(z)
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-7-7ba47b19f282> in <module>()
    ----> 1 itemgetter(id)(z)
    
    TypeError: list indices must be integers, not list
    

How can I make itemgetter to take input from list variable correctly, i.e.
using `id`?


Answer: When you do:

    
    
    print itemgetter(id)(z)
    

you are passing a `list` to `itemgetter`, while it expects indices (integers).

**What can you do?** You can unpack the `list` using `*`:

    
    
    print itemgetter(*id)(z)
    

to visualize this better, both following calls are equivalent:

    
    
    print itemgetter(1, 2, 3)(z)
    print itemgetter(*[1, 2, 3])(z)
    

