multidimensional lists

Why not to allow tuple as a list index?
https://stackoverflow.com/questions/40361743/python-using-tuples-as-list-ind...

On Wed, Jul 8, 2020 at 11:18 PM Hans Ginzel <hans@matfyz.cz> wrote:
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

On Wed, Jul 08, 2020 at 11:32:32PM +1000, Chris Angelico wrote:
Thank you. Great idea. Such function could be made default method for any subscriptable. :-) H. PS: For dicts there is a better solution, Munch, munchify(), https://github.com/Infinidat/munch

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

On Wed, Jul 8, 2020 at 11:18 PM Hans Ginzel <hans@matfyz.cz> wrote:
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

On Wed, Jul 08, 2020 at 11:32:32PM +1000, Chris Angelico wrote:
Thank you. Great idea. Such function could be made default method for any subscriptable. :-) H. PS: For dicts there is a better solution, Munch, munchify(), https://github.com/Infinidat/munch

On 08.07.20 15:09, Hans Ginzel wrote:
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])
participants (3)
-
Chris Angelico
-
Dominik Vilsmeier
-
Hans Ginzel