Newbie: The philosophy behind list indexes

John Ladasky john_ladasky at sbcglobal.net
Sat Jun 15 03:19:20 EDT 2013


On Friday, June 14, 2013 10:21:28 PM UTC-7, ian.l.... at gmail.com wrote:
>I'm sure there's a good reason, but I'm worried it will result in a lot of 
>'one-off' errors for me, so I need to get my head around the philosophy of this
>behaviour, and where else it is observed (or not observed.)

My understanding is that it's so the same index can be used in slicing a list into complementary parts.  This also makes concatenation the inverse operation of slicing:


Python 3.3.1 (default, Apr 17 2013, 22:30:32) 
[GCC 4.7.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a = list(range(10))
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a[:7]
[0, 1, 2, 3, 4, 5, 6]
>>> a[7:]
[7, 8, 9]
>>> a[:7] + a[7:]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


It's a logical extension of the start-indexing-at-zero issue, when you think about it.  Yes, you will make the occasional off-by-one error at first.  You'll get over it.



More information about the Python-list mailing list