On 08.07.20 15:09, Hans Ginzel 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

Numpy offers this convenience for multi-dimensional arrays. But in
Python the sub-lists don't need to have the same length, so for example

    T = [[1, 2, 3], [4], [5, 6, 7]]

and then `T[1, 2]` doesn't even exist. Also it could similarly mean "get
the second and third element from the list" which is what
`operator.itemgetter` does:

    >>> operator.itemgetter(1, 2)(T)
    ([15, 6, 10], [10, 8, 12, 5])
_______________________________________________
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/ILW2YJ73HLHOXQCGEYZRDW7LNJSOGW5S/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to