Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I don't think it is intuitive for

my_list[0:3]

to be the first three elements of the list.

Go from position 0 to position 3 should be 0, 1, 2, 3




It's nice that my_list[0:3] + my_list[3:6] == my_list[0:6]

It's nice that 3-0 = 3 elements.

It's nice that an index counts the number of elements before it.

It's nice that my_list[0:length] is the whole list, with no extra arithmetic needed.

So many nicer things about this way of doing things.


I think it is just personal preference.

I switched from Matlab to Pyhthon and I am totally happy that Python starts counting at 0 (which is very important if you do image processing). But even after four years of working Python with the open interval notation of the last element it seems wrong to me. I think it is related to the fact that the Matlab version is more direct. a[1:3] means: give me the elements 1, 2, 3. That is very clear and obvious. In python a[1:3] means: give me the element 1 and 2. This is not that obvious and even after four years of python sometimes I have to stop and think about it.


I envision the array on a continuous axis, and place markers at 1 and 3 (the exact positions, not the range of whole numbers). The range on the axis between them is the right range. With this intuition, everything is very simple and makes perfect sense.


Yes, it makes perfect sense, but it still feels wrong for me. I guess it is just related to the fact that I grew up with Matlab and not Python. I was kind of hoping I will get used to it but even after several years of writing image processing stuff in Python I am still not a 100% convinced.


Agreed. I find the half-open ranges impossible and stumble over them every time.

They are inconsistent within two characters: closed on one side, then open on the other.

If the notation was >my_list[0:3) at least it would be mathematically appropriate.


See the Dijkstra essay[1] for a convincing argument for half-open ranges.

https://www.cs.utexas.edu/~EWD/transcriptions/EWD08xx/EWD831...


I have read it, and Dijkstra is a good call to authority, but I don't agree.

His opening sentence is instructive: using the 'pernicious three dots', everything is totally clear: with an ellipsis, it's impossible to understand anything other than a closed interval.

It's primary school mathematics that 2 <= x <= 12, i.e. the interval mathematically written [2,12] (or 2,...,12), contains 11 integers. A programmer who doesn't understand that is in a world of trouble as half-open intervals won't stop the inevitable rain of off-by-one errors.

A programmer who actually calculates the size of a list in the way that Dijkstra suggests (specifically, by calculating b-a) is also perhaps acting questionably. Dijkstra's argument here might make more sense when correctly framed in 1982.

As an aside, if Dijkstra would agree to use a fully-closed interval, his suggested advantage of 0-indexing is eliminated and he would presumably then prefer 1-indexing.


That's not the entirety of the argument. There's the natural number stuff, as well.

Also, b-a is appealing because simplicity is a good indicator you're on the right track, and part of the definition of elegance.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: