I'm running a script that parses points from a flat database structure into individual feature classes with related tables.

A search cursor runs through and stores each field's data in a variable that I use to create points with using an insert cursor. After each loop, I'd like to clean up the variables so that data from the last loop can't be used in each loop thereafter.

I've got an array that I've hard coded with each variable name that I loop through to delete each one. I've checked these var names 100 times to make sure they're correct (even copied them from where they're declared and pasted them into the array).

I'm getting an error:

IndexError: list index out of range

I'm using len() to find the length of the array, so I can't imagine how it could be out of range.

    # Clean-up Variables
    cleanup = [ptGeom, ptName, ptType, ptDate, active, ptComm, fdDate, fdCheck, surType,
               fdType, special, lure1, lure2, fdComm, photo, signType, signAmt, platSet,
               platSign, trapType, trapAct, trapStat, capSpec, capSex, capType, trapSet]
    length = len(cleanup) - 1

    for i in range(0, length):
        print i
        if cleanup[i]:
            del cleanup[i]

The error is after it prints 19 (trapType) -- which is a null value according to the attribute table. The loop isn't having trouble with any of the other null values that it encounters before this line.

  • 1
    I don't know Python, so please take these remarks as speculative. It seems there are two problems. First, the code displayed appears to create a new array cleanup, populates it with the current values of 26 variables (not their names), and then attempts to delete the non-null entries. I would guess that each successful deletion shortens the length of cleanup. By the time you get to trapType, you have probably deleted seven entries, whence index 19 falls out of range. Second, it doesn't matter: this code won't do a thing to those 26 variables; it only modifies cleanup.
    – whuber
    Apr 10, 2012 at 20:31
  • Thanks, I hadn't considered that I was deleting the values as opposed to the variables themselves.
    – Roy
    Apr 11, 2012 at 11:13

1 Answer 1


You really don't have to bother with all those deletes, this is a sort of ugly antipattern perpetuated in the docs. The only objects that need explicit deletes are cursors and rows, and that's just so you can get around the occasionally non-deterministic nature of the garbage collector causing schema locks to be held longer than expected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.