954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

IndexError: list index out of range

testingdays = testingData.getMeasurements()

for day in testingdays:
	dayValues = []
	dayValues[0].append(day.tempMean)
	dayValues[1].append(day.tempMax)
	dayValues[2].append(day.tempMin)
	dayValues[3].append(day.dewPoint)
	dayValues[4].append(day.humidMean)	
	dayValues[5].append(day.humidMax)
	dayValues[6].append(day.humidMin)
	dayValues[7].append(day.pressure)
	dayValues[8].append(day.meanWindSpeed)
	dayValues[9].append(day.maxWindSpeed)
	dayValues[10].append(day.maxGustSpeed)
	dayValues[11].append(day.visibility)


this is my code and when i try to run it i get the error message:

"Traceback (most recent call last):
File "main.py", line 166, in
dayValues[0].append(day.tempMean)
IndexError: list index out of range"

Each day has 12 classes/elements so the list index shouldn't be out of range!
I've got code just before this which pops items from a list, but this part shouldn't be involved with that so i'm not sure why i get this error message
sorry, not sure if i've posted enough information to get help with this!
Thanks

spe_eddy
Newbie Poster
13 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

dayValues is an empty list when you start, so dayValues[0] isn't yet in dayValues and 0 is an index out of range.
You probably want dayValues = [[]]*12 at line 4

griswolf
Veteran Poster
1,165 posts since Apr 2010
Reputation Points: 344
Solved Threads: 256
 

just append your data;

testingdays = testingData.getMeasurements()
dayValues = []
for day in testingdays:
    dayValues.append(day.tempMean)
    dayValues.append(day.tempMax)
    dayValues.append(day.tempMin)
    dayValues.append(day.dewPoint)
    dayValues.append(day.humidMean)    
    dayValues.append(day.humidMax)
    dayValues.append(day.humidMin)
    dayValues.append(day.pressure)
    dayValues.append(day.meanWindSpeed)
    dayValues.append(day.maxWindSpeed)
    dayValues.append(day.maxGustSpeed)
    dayValues.append(day.visibility)


And you are ready to go. And dont put or initialize list in a loop ok?
;)

richieking
Master Poster
764 posts since Jun 2009
Reputation Points: 61
Solved Threads: 152
 

thanks that helps a lot, i tried the second option, works now,
and i need the list to be initialised in a loop because i need it to reset for each day (there's more code below it within the for loop)

spe_eddy
Newbie Poster
13 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

But that is not the logic.
You need to reset the list container or delete.

But you dont have to initialize a list in a loop.
no good ;)

richieking
Master Poster
764 posts since Jun 2009
Reputation Points: 61
Solved Threads: 152
 

I have difficulty understanding why you do not leave values in the object. From variable name I would expect to be list of measurement objects.

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: