short form: Let’s have something called like
lix() that is defined as
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