indexerror in python with beaglebone black

74 views Asked by At
y[i] = ADC.read("P9_40")
IndexError: list assignment index out of range

the code:

i = 1
x = []*1000
y = []*1000
for i in range(1000): 
        y[i] = ADC.read("P9_40") # adc input pin
        x[i] = (int)(y*2147483648) # conversion of float to int

this code is read data from analogue pin of beaglebone black and store the result in array

2

There are 2 answers

0
The6thSense On

In python you can't call a uncreated index of a list

x = []*1000 #creates only one empty list not 1000 list of list
y = []*1000 #like wise here

It should rather be like this

x = [[] for i in range(1000)]
y = [[] for i in range(1000)]
0
Anthon On

You don't create a 1000 element list by doing:

x = [] * 1000
y = [] * 1000

What you should do is create it with None values:

x = [None] * 1000
y = [None] * 1000

and then you can overwrite those None in your loop. None is better than any integer value (like 0) that might come out of ADC.read(), as you can check if the whole list is updated afterwards.