7 Replies - 3800 Views - Last Post: 01 February 2010 - 06:03 PM Rate Topic: -----

#1 chemicalfan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 88
  • Joined: 16-October 09

Python IndexError exception - layman's terms?

Post icon  Posted 25 January 2010 - 03:26 AM

I've got a program which is throwing an IndexError exception, but I don't know why. I've recycled some old code I had (I couldn't import it for some reason, so I've cut and paste the code I needed - not ideal). The Python Library Reference states:

Quote

Raised when a sequence subscript is out of range. (Slice indices are silently truncated to fall in the allowed range; if an index is not a plain integer, TypeError is raised.)


Err, what? I don't understand that definition in terms of my code - the method is calling a global function to return a list, and then passing that list to another method to write the row to a CSV file (both calls are under the 'try' statement. I'm not sure which parts of code I need to post up at the moment - can someone help me understan what this exception is all about?

Is This A Good Question/Topic? 0
  • +

Replies To: Python IndexError exception - layman's terms?

#2 dorknexus  Icon User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1256
  • View blog
  • Posts: 4,618
  • Joined: 02-May 04

Re: Python IndexError exception - layman's terms?

Posted 25 January 2010 - 03:49 AM

It means that one of your methods is trying to access an element that doesn't exist in the list. An example would be an list of 10 items and you try to access the 11th or the -1st.
Was This Post Helpful? 1
  • +
  • -

#3 chemicalfan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 88
  • Joined: 16-October 09

Re: Python IndexError exception - layman's terms?

Posted 25 January 2010 - 06:04 AM

I'm an idiot.

The global function that builds the list from another list, selects up to element 12, when there are only 11 elements in the list (so should stop at 10 really!)

Thanks for you help anyway!
Was This Post Helpful? 0
  • +
  • -

#4 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Python IndexError exception - layman's terms?

Posted 01 February 2010 - 05:02 PM

View PostDark_Nexus, on 25 Jan, 2010 - 02:49 AM, said:

It means that one of your methods is trying to access an element that doesn't exist in the list. An example would be an list of 10 items and you try to access the 11th or the -1st.



Not to step on your toes, but in python, it's completely fine trying to access the -1st element of a list. In any python list, that will return the last element in the list.

-Adam
http://www.learningp...rogramming.com/
Was This Post Helpful? 0
  • +
  • -

#5 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,050
  • Joined: 15-July 08

Re: Python IndexError exception - layman's terms?

Posted 01 February 2010 - 05:08 PM

View Postatraub, on 1 Feb, 2010 - 06:02 PM, said:

View PostDark_Nexus, on 25 Jan, 2010 - 02:49 AM, said:

It means that one of your methods is trying to access an element that doesn't exist in the list. An example would be an list of 10 items and you try to access the 11th or the -1st.



Not to step on your toes, but in python, it's completely fine trying to access the -1st element of a list. In any python list, that will return the last element in the list.

-Adam
http://www.learningp...rogramming.com/


I think in this case, he meant the index BEFORE the 0th index, not the last element.
Was This Post Helpful? 0
  • +
  • -

#6 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Python IndexError exception - layman's terms?

Posted 01 February 2010 - 05:58 PM

Quote

I think in this case, he meant the index BEFORE the 0th index, not the last element.


I think I might be misunderstanding what you're saying.

I realize that trying to access an item before the 0th position might cause unexpected output but it won't throw an IndexError, unless you go way too far negative. I'm not trying to split hairs, I just want to make sure I'm not missing anything.

#Adam
http://www.learningp...rogramming.com/

This post has been edited by atraub: 01 February 2010 - 06:01 PM

Was This Post Helpful? 0
  • +
  • -

#7 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,050
  • Joined: 15-July 08

Re: Python IndexError exception - layman's terms?

Posted 01 February 2010 - 06:00 PM

Oh, Python...sorry, I zoned. I've been working in Java too long. Yes, you're right, if you access the -1 index of a list, then it will return the last index, UNLESS your list is empty:

>>> list = []
>>> list[-1]
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
	list[-1]
IndexError: list index out of range


Was This Post Helpful? 0
  • +
  • -

#8 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Python IndexError exception - layman's terms?

Posted 01 February 2010 - 06:03 PM

Haha you got me there, I didn't think of an empty list. =)


#Adam
http://www.learningp...rogramming.com/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1