By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,923 Members | 1,655 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,923 IT Pros & Developers. It's quick & easy.

Indexing a list, the safe way

bartonc
Expert 5K+
P: 6,596
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
Share this Question
Share on Google+
3 Replies


kudos
Expert 100+
P: 126
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
Expert 5K+
P: 6,596
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
Expert 100+
P: 126
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.