Modifying Element In For List

octopusgrabbus old_road_farm at verizon.net
Mon Nov 15 11:31:52 EST 2010


My question concerns elementary list and pass by reference:

I've written a function which is passed a list that contains rows read
from a csv file. The function traverses csv_rows, row by row, and
inspects the first element in each row. The function tests for '', and
if true, replaces that with a 0.

I've used the standard Python for syntax for this.

def cleanMeterID(csv_rows, bad_meter_id_count):
    d = drIdx()

    row_number = 0

    for row in csv_rows:
        if False == is_number(row[d.MeterID]):
            csv_rows[row_number][d.MeterID] = 0
            row_number = row_number + 1
            bad_meter_id_count[0]= bad_meter_id_count[0] + 1

    print("Received ", bad_meter_id_count[0], " bad meter ids")

I believe the logic show above is flawed, because I am not modifying
elements in the original csv_rows list. I would modify this to use an
index to traverse the list like

idx=None
for idx in range(0, len(csv_rows), 1):
        if False == is_number(row[d.MeterID]):
            csv_rows[row_number][d.MeterID] = 0
            row_number = row_number + 1
            bad_meter_id_count[0]= bad_meter_id_count[0] + 1

Is this correct?

Thank you.



More information about the Python-list mailing list