On Wed, Jul 8, 2020 at 11:18 PM Hans Ginzel <[email protected]> wrote:
>
> Why not to allow tuple as a list index?
>
> >>> T = [[11, 12, 5, 2], [15, 6, 10], [10, 8, 12, 5], [12, 15, 8, 6]]
> >>> print(T[1][2])
> 10
> >>> print(T[1, 2])
> Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
> TypeError: list indices must be integers or slices, not tuple
>
> https://stackoverflow.com/questions/40361743/python-using-tuples-as-list-indices

Because you're not indexing a list with a two-dimensional value.
You're indexing a list with a single number, which gives you another
list, which you then index with another value. If you want a helper
function, it's not hard to write it:

def fetch(collection, indices):
    for index in indices:
        collection = collection[index]
    return collection

Job done. This doesn't belong on the list type, because you might want
the exact same behaviour starting with (or incorporating) a dict, or
any other type that can be subscripted.

ChrisA
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/BRTQGAGTLHLJTSQHHPFFZJFVGIL4Q7WO/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to