469,293 Members | 1,364 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,293 developers. It's quick & easy.

Indexing a list, the safe way

bartonc
6,596 Expert 4TB
Whenever a function retrieves a list element outside of loop, you should wrap the reference in a try block instead of testing the length of the list (because you will probably forget to subtract one in your test, introducing a but). For example:
These are safe:
Expand|Select|Wrap|Line Numbers
  1.  
  2. # inside a loop
  3. for item in someList:
  4.     # use items from list sequentially
  5.  
  6. try:
  7.     item = someList[index]
  8. except IndexError:
  9.     # handle out-ot-range index
  10.  
  11. if index < len(someList):
  12.     item = someList[index]
  13. else:
  14.     # handle out-of-range index
  15.  
This is a bug that will give an error sometimes:
Expand|Select|Wrap|Line Numbers
  1.  
  2. if index > len(someList): # should be len(someList) - 1
  3.     # handle out-ot-range index
  4. else:
  5.     item = someList[index]
  6.  
As you can see, both the if block and the try block have the same number of lines and structure, but try reduces the number of references to someList, doesn't need to call len() and protects you from a simple mistake.
Nov 19 '06 #1
3 4737
kudos
127 Expert 100+
Hi,
its not a bug the indexing in languages like python, C, java indexes from 0 as the first. So, in a = [1,2,3] elements a[2] = 3, but is still contains 3 elements..
If I recall correctly pascal & matlab indexes from 1 (which actually is more "mathematical".

-kudos


Whenever a function retrieves a list element outside of loop, you should wrap the reference in a try block instead of testing the length of the list (because you will probably forget to subtract one in your test, introducing a but). For example:
These are safe:
Expand|Select|Wrap|Line Numbers
  1.  
  2. # inside a loop
  3. for item in someList:
  4.     # use items from list sequentially
  5.  
  6. try:
  7.     item = someList[index]
  8. except IndexError:
  9.     # handle out-ot-range index
  10.  
  11. if index < len(someList):
  12.     item = someList[index]
  13. else:
  14.     # handle out-of-range index
  15.  
This is a bug that will give an error sometimes:
Expand|Select|Wrap|Line Numbers
  1.  
  2. if index > len(someList): # should be len(someList) - 1
  3.     # handle out-ot-range index
  4. else:
  5.     item = someList[index]
  6.  
As you can see, both the if block and the try block have the same number of lines and structure, but try reduces the number of references to someList, doesn't need to call len() and protects you from a simple mistake.
Nov 19 '06 #2
bartonc
6,596 Expert 4TB
Hi,
its not a bug the indexing in languages like python, C, java indexes from 0 as the first. So, in a = [1,2,3] elements a[2] = 3, but is still contains 3 elements..
If I recall correctly pascal & matlab indexes from 1 (which actually is more "mathematical".

-kudos
But len() returns the lenght of the object which IS one greater than the index of the last item: len([1,2,3]) = 3
Nov 20 '06 #3
kudos
127 Expert 100+
yeah, I know. I guess it would make more sense if indexing started at 1, so one could get the last elements by doing a len, but for some reason indexing start at 0. I guess its just tradition, C index start at 0, so then everybody else do it too...(but its true, ALOT of bugs had been avoided we started at 1)

-kudos

But len() returns the lenght of the object which IS one greater than the index of the last item: len([1,2,3]) = 3
Nov 20 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

21 posts views Thread by Hilde Roth | last post: by
12 posts views Thread by Steven Bethard | last post: by
10 posts views Thread by Ishwor | last post: by
108 posts views Thread by Bryan Olson | last post: by
6 posts views Thread by Zri Man | last post: by
6 posts views Thread by yomgui | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.