A command to return the maximum index of a list or array - the better len

Hello :smiley:,

short form: Let’s have something called like lix() that is defined as len()-1, please!
def lix(obj, /): return len(obj) - 1

Motivation: When I started coding in python a few years ago, I did not understand the general use, why len() returns one more than the maximum valid index. Yes it makes sense from the linguistic approach, but coding wise at least I was ever more so often interested in the maximum index of e.g. MyList. However a few hours into coding I was okay with using len()-1 in those cases. I guess most get used to this very fast.
But yesterday I watched The Worst Programming Language Ever - Mark Rendle - NDC Oslo 2021 - YouTube. and somewhere in his talk he points out that this inconvenience exists in any programming language, despite the fact one wants to know the maximum index of a list or array in most of the cases.
So now, that I am not alone on this and recall my first thoughts about this, I asked myself: Why not improve on this? I will definitely define lix() for myself in my codes and I don’t know a good reason, why this should not be an improvement. It is a very lightweight but neat feature and any newcomer will certainly find this useful too.

Why the name lix()? I thought combining len, “index” and also maybe “maximum”, lix() would make a memorisable and sufficiently easy to spell command. However I certainly love to see an even better command for this! After all it is about convenience.

Disclamer: I am absolutely no expert in python and don’t know how big of a deal it is to add something to the standard library. However (programming) languages are changing over time and this simple change seemed apparent when I started coding until I got used to it.
I also don’t want to say python is a bad programming language at all. I actually think quite the opposite is true and I had chosen python as my entry into programming when I started because of its very good reputation.

Happy to know what your thoughts are :slightly_smiling_face:

Adding a function like lix is not needed because Python sequences support indexing from the end, so the maximum valid index is simply -1.

9 Likes

What are the use-cases of lix() that can’t be solved by negative indexing? Isn’t my_list[lix(my_list)] the same as my_list[-1] but longer?

This may highlight a missed opportunity to define len() as max_index() versus its current definition of count().

In pseudocode…
len() returns the equivalent of ~.count(*) where ‘*’ is a wildcard. Therefore, we could have defined list.count() with no argument as a “count all members” default behavior (and still could). This would free up len()to return a directly indexible result. (Of course, len() is not only used for lists, but also strings, tuples, sets, and dictionaries. Arrays, also. So such a migration would have to be carefully planned and executed.

Redefining len()would break a lot of code but count() as a built-in function isn’t defined as of Python3.10 so could be put in place as a replacement for len(). After 20 years or so, perhaps len() could be redefined to return the index value without breaking much–perhaps not any–legacy code. at that point the users should know not to try to run the code on anything later than Python 4.9 or something.

Yes, even the modern programmer quickly realizes that a common bug is that your pointer is either one step past your target or one step short of it. Maybe we can evolve to a higher state… :innocent:

(post deleted by author)

Taking this post at face value, I’m a little confused. Supposing for a moment that we were going to define a whole new builtin for this, why wouldn’t we instead simply define a max_index() builtin instead and leave len() alone, which would solve the posed problem now instead of in 20 years, not require changing the behavior of an existing builtin and breaking existing code, avoid two redundant builtins that do the same thing, and not confuse existing or future users about why len does not return the length of their collection. This would seem to provide a superset of the benefit for a subset of the cost—have I misunderstood something?

1 Like