osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Enumerate - int object not subscriptable


On 2019-08-20 2:00 PM, Sayth Renshaw wrote:
> Hi
> 
> I want to do basic math with a list.
> 
> a = [1, 2, 3, 4, 5, 6, 7, 8]
> 
> for idx, num in enumerate(a):
>      print(idx, num)
> 
> This works, but say I want to print the item value at the next index as well as the current.
> 
> for idx, num in enumerate(a):
>      print(num[idx + 1], num)
> 
> I am expecting 2, 1.
> 
> But am receiving
> 
> TypeError: 'int' object is not subscriptable
> 
> Why?
> 

I think you want a[idx+1], not num[idx+1].

Bear in mind that you will get IndexError for the last item in the list.

Frank Millman