This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Use starred expressions in subscripts
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.11
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Tuple unpacking in getitem
View: 43812
Assigned To: Nosy List: Dennis Sweeney, PeterTillema, ajoino
Priority: normal Keywords:

Created on 2021-10-23 08:58 by PeterTillema, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (7)
msg404860 - (view) Author: Peter Tillema (PeterTillema) Date: 2021-10-23 08:58
It would be nice if you could starred expressions in list indices, for example this piece of code:

```
import numpy as np

a = np.array(0)
print(a[1, *[2, 3], 4])
```
msg404908 - (view) Author: Jacob Nilsson (ajoino) Date: 2021-10-23 20:06
I don't understand, do you mean that lists should work like in your example? Or that your example code doesn't run?

If you mean the first issue, that is ok I guess but I've never used indexing like that outside of numpy, pandas and the like.

If you mean the second issue, it doesn't run because you are indexing a 0-dim array with 4 indices, if you instead try with a 1-dim array:

>>> import numpy as np
>>> a = np.array([0]) # 1-dim array instead of 0-dim
>>> print(a[[0, *[0, 0], 0]])
[0, 0, 0, 0]

You get the expected output.
msg404909 - (view) Author: Jacob Nilsson (ajoino) Date: 2021-10-23 20:11
Oh yeah, the reason lists don't allow the starred expression has nothing to do with the starred expression itself, it's syntactically correct and in your case a[1, *[2, 3], 4] is equivalent to a[1, 2, 3, 4]. The "problem" is that lists do not allow indexing by tuples.

Perhaps the title of this issue should be changed to reflect that: "Allow lists to be indexed by tuples"
msg404915 - (view) Author: Dennis Sweeney (Dennis Sweeney) * (Python committer) Date: 2021-10-24 02:45
Jacob wrote:
    a[[0, *[0, 0], 0]]
which passes the *list* [0, 0, 0, 0] into __getitem__. In numpy, passing lists into __getitem__ does things like array([1, 10, 100])[[2, 1]] --> array([100, 10]).

But I think Peter is requesting that the following work:
    a[0, *[0, 0], 0]
    (this is currently a SyntaxError)
and the proposal is that it would pass the *tuple* (0, 0, 0, 0) into __getitem__, indexing into each dimension of the array.

A workaround is to use a[(0, *[0, 0], 0)] -- just one extra pair of parentheses.

This is a duplicate of https://bugs.python.org/issue43812 , where it was pointed out that this is currently being proposed as part of https://www.python.org/dev/peps/pep-0646/#implications , which it appears is in the queue of things for the steering council to rule on. I'll close this as a duplicate for now, but feel free to re-open if you feel something is missing from that PEP.
msg404954 - (view) Author: Peter Tillema (PeterTillema) Date: 2021-10-25 08:34
Right, I should have clarified it a bit more. I'm using NumPy arrays because they allow indexing like this, where the input arguments are converted to a tuple. So
   a[1, 2, *[3, 4]]
is different than
   a[[1, 2, *[3, 4]]]

This indeed only works on NumPy arrays, although I would like to see such feature implemented for real lists!
msg405029 - (view) Author: Jacob Nilsson (ajoino) Date: 2021-10-26 07:53
Ok, I see.

>>> a[1, 2, *[3, 4]]
Would still faith with PEP 646 because lists don't accept tuples, right?
>>> a[(1, 2, *[3, 4])]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list indices must be integers or slices, not tuple
msg405062 - (view) Author: Dennis Sweeney (Dennis Sweeney) * (Python committer) Date: 2021-10-26 23:05
Yes, I don't believe the PEP is suggesting any changes to list.__getitem__. If you want to suggest such enhancements, I would suggest first sending a message to the python-ideas mailing list or on discuss.python.org.
History
Date User Action Args
2022-04-11 14:59:51adminsetgithub: 89749
2021-10-26 23:05:06Dennis Sweeneysetmessages: + msg405062
2021-10-26 07:53:36ajoinosetmessages: + msg405029
2021-10-25 08:34:21PeterTillemasetmessages: + msg404954
2021-10-24 02:45:48Dennis Sweeneysetstatus: open -> closed

superseder: Tuple unpacking in getitem
title: Use starred expressions in list indices -> Use starred expressions in subscripts
nosy: + Dennis Sweeney

messages: + msg404915
resolution: duplicate
stage: resolved
2021-10-23 20:11:41ajoinosetmessages: + msg404909
2021-10-23 20:06:00ajoinosetnosy: + ajoino
messages: + msg404908
2021-10-23 09:06:45AlexWaygoodsettype: enhancement
versions: + Python 3.11, - Python 3.10
2021-10-23 08:58:24PeterTillemacreate