[Python-ideas] unpacking in index expression

Serhiy Storchaka storchaka at gmail.com
Thu Apr 7 00:39:54 EDT 2016

On 07.04.16 01:26, Alexander Heger wrote:
> I was trying to unpack an array or tuple in index expression (python
> 3.5.1) but this failed.
> I think for consistency of language use it would be desirable to allow
> that as well.  Below an example of my attempt indexing a numpy array,
> x:
>>>> import numpy as np
>>>> x = np.arange(12).reshape(3,-1)
>>>> x[*[0,1]]
>      x[*[0,1]]
>        ^
> SyntaxError: invalid syntax

x[0, 1] is a syntax sugar for x[(0, 1)]. Thus you need just convert your 
list index to tuple index: x[tuple([0, 1])].

Unpacking arguments for function is different thing.

More information about the Python-ideas mailing list