Indexing a list, the safe way

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    Indexing a list, the safe way

    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:
    Code:
     
    # inside a loop
    for item in someList:
    	# use items from list sequentially
     
    try:
    	item = someList[index]
    except IndexError:
    	# handle out-ot-range index
     
    if index < len(someList):
    	item = someList[index]
    else:
    	# handle out-of-range index
    This is a bug that will give an error sometimes:
    Code:
     
    if index > len(someList): # should be len(someList) - 1
    	# handle out-ot-range index
    else:
    	item = someList[index]
    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.
  • kudos
    Recognized Expert New Member
    • Jul 2006
    • 127

    #2
    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 "mathematic al".

    -kudos


    Originally posted by bartonc
    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:
    Code:
     
    # inside a loop
    for item in someList:
    	# use items from list sequentially
     
    try:
    	item = someList[index]
    except IndexError:
    	# handle out-ot-range index
     
    if index < len(someList):
    	item = someList[index]
    else:
    	# handle out-of-range index
    This is a bug that will give an error sometimes:
    Code:
     
    if index > len(someList): # should be len(someList) - 1
    	# handle out-ot-range index
    else:
    	item = someList[index]
    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.

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by kudos
      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 "mathematic al".

      -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

      Comment

      • kudos
        Recognized Expert New Member
        • Jul 2006
        • 127

        #4
        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

        Originally posted by bartonc
        But len() returns the lenght of the object which IS one greater than the index of the last item: len([1,2,3]) = 3

        Comment

        Working...