08-07-2013, 12:43 AM
(08-05-2013, 09:16 PM)JeremyG Wrote:Code:a=[1,2,3,4,5]
print([a[i+1] for i in range(len(a)-1)])
I honestly have no clue. This outputs [2,3,4,5]
It's because the a array is zero indexed. The i value itself starts at zero, but because of this bit here:
a=[1,2,3,4,5]
print([a[i+1] for i in range(len(a)-1)])
it starts the array's index at 1 instead of zero. Lucky that the range comes out to 4, otherwise you might end up trying to access a nonexistent element.