John Salerno wrote: > Given: > > numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] > > can someone explain to me why > > numbers[10:0:-2] results in [10, 8, 6, 4, 2]?
I always have trouble with these. Given the docs[1]: """ The slice of s from i to j with step k is defined as the sequence of items with index x = i + n*k such that $0 \leq n < \frac{j-i}{k}$. In other words, the indices are i, i+k, i+2*k, i+3*k and so on, stopping when j is reached (but never including j). If i or j is greater than len(s), use len(s). If i or j are omitted then they become ``end'' values (which end depends on the sign of k). Note, k cannot be zero. """ I would expect that we're looking at the indices 10 + n*(-2) such that 0 <= n < (0-10)/-2 0 <= n < 5 which should be indices 10, 8, 6, 4, 2 which would skip 10 so as not to provoke an IndexError, and then give you the list [9, 7, 3, 5, 1]. Clearly that's not what happens. Looks like there's a rule in there somewhere that says if the start is greater than the length, use the length instead. That would mean starting at 9 instead of 10, and getting the indices 9, 7, 5, 3, 1 which would give you the list [10, 8, 6, 4, 2] Hmm... Is there some documentation I'm missing that says the start index will be replaced with the length if necessary? I'll file a doc patch if it looks like I'm not just missing something somewhere else... STeVe [1]http://docs.python.org/lib/typesseq.html -- http://mail.python.org/mailman/listinfo/python-list