[GIS] Index out of range error when cleaning up variables

arcpypython

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.

Best Answer

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.

Related Question