Skip to main content
Post Reopened by PolyGeo
edited tags; edited title
Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345

Update Cursor with Lists as Dictionary?

deleted 1816 characters in body; edited tags; edited title
Source Link
saoirse
  • 246
  • 2
  • 13

Update Cursor with Lists gives IndexError: list index out of range?as Dictionary

I have a field, "Access" that I want to populate with 4 variables based on the attribute of another field, "Des_Tp".

import arcpy
from arcpy import env

env.overwriteOutput = True

inFeatures = r"C:\Users\A\AccessTest.gdb\Access_test"

OA_list = ["NP","NM","NCA","NF","NG","PUB","NT","WSR","NRA","NLS","IRA","ACEC","REC","RMA","ACC","OCS","SP","SREC","SLHCA","SRMA","LP","LREC","LHCA","LRMA"]
RA_list = ["NWR","WA","WSA","NSBV","RNA","REA","HCA","SDA","SW","SCA","LCA"]
UK_list = ["MPA","PROC","FOTH","ND","TRIBL","SO","LO","UNK"]
XA_list = ["WPA","MIT","MIL","PCON","PREC","PHCA","PAGR","PRAN","PFOR","PO","PO","CONE","RECE","HCAE","HCAE","AGRE","RANE","FORE","OTHE","UNKE"]

fields = ['Des_Tp', 'Access']

expression = '"Access_Src"'  + "='" + "GAP - Default" + "'"
dm.MakeFeatureLayer(inFeatures, "TEMP_LYR")
dm.SelectLayerByAttribute ("TEMP_LYR", "NEW_SELECTION", expression)  

for i in range (0,63):
    with arcpy.da.UpdateCursor ("TEMP_LYR", fields) as cursor:
        for row in cursor:
            if row [0] == OA_list[i]:
                row [1] = "OA"
            elif row [0] == RA_list[i]:
                row [1] = "RA"
            elif row [0] == UK_list[i]:
                row [1] = "UK"
            elif row [0] == XA_list[i]:
                row [1] = "XA"
            cursor.updateRow(row)
del cursor

dm.Delete("TEMP_LYR")

ForFor example, if a feature withhas "Des_Tp" = 'NP" will be assigned an access of "OA". When I run the code'NP', I encounter the error

Traceback (most recent call last):
  File "C:/Users/A/Desktop/Access_test.py", line 34, in <module>
    elif row [0] == UK_list[i]:
IndexError: list index out of range

then I currently designate (0,63)would want to code it as the range because that is how many variables I haveopen access, 'OA' in the "Des_Tp" field, but I may be interpreting this line incorrectly "Access".

As Paul commented below, it might be possible to simplify the code with a dictionary. I've put them listthe lists in a dictionary, but the code runs without any change in the data:

import arcpy
from arcpy import env

arcpy.env.overwriteOutput = True

inFeatures = r"C:\Users\A\AccessTest.gdb\Access_test"
destp_dict ={1:("NP","NM","NCA","NF","NG","PUB","NT","WSR","NRA","NLS","IRA","ACEC","REC","RMA","ACC","OCS","SP","SREC","SLHCA","SRMA","LP","LREC","LHCA","LRMA"),
2:("NWR","WA","WSA","NSBV","RNA","REA","HCA","SDA","SW","SCA","LCA"),
3:("MPA","PROC","FOTH","ND","TRIBL","SO","LO","UNK"),
4:("WPA","MIT","MIL","PCON","PREC","PHCA","PAGR","PRAN","PFOR","PO","PO","CONE","RECE","HCAE","HCAE","AGRE","RANE","FORE","OTHE","UNKE")}

fields = ['Des_Tp', 'Access']
access_type = ["OA", "RA", "UK", "XA"]

dmarcpy.MakeFeatureLayer(inFeatures, "TEMP_LYR")

with arcpy.da.UpdateCursor ("TEMP_LYR", fields) as cursor:
    for row in cursor:
        if row[0] == destp_dict[1]:
            row[1] = "OA"
        if row[0] == destp_dict[2]:
            row[1] = "RA"
        if row[0] == destp_dict[3]:
            row[1] = "UK"
        if row[0] == destp_dict[4]:
            row[1] = "XA"
        cursor.updateRow(row)

Update Cursor with Lists gives IndexError: list index out of range?

I have a field, "Access" that I want to populate with 4 variables based on the attribute of another field, "Des_Tp".

import arcpy
from arcpy import env

env.overwriteOutput = True

inFeatures = r"C:\Users\A\AccessTest.gdb\Access_test"

OA_list = ["NP","NM","NCA","NF","NG","PUB","NT","WSR","NRA","NLS","IRA","ACEC","REC","RMA","ACC","OCS","SP","SREC","SLHCA","SRMA","LP","LREC","LHCA","LRMA"]
RA_list = ["NWR","WA","WSA","NSBV","RNA","REA","HCA","SDA","SW","SCA","LCA"]
UK_list = ["MPA","PROC","FOTH","ND","TRIBL","SO","LO","UNK"]
XA_list = ["WPA","MIT","MIL","PCON","PREC","PHCA","PAGR","PRAN","PFOR","PO","PO","CONE","RECE","HCAE","HCAE","AGRE","RANE","FORE","OTHE","UNKE"]

fields = ['Des_Tp', 'Access']

expression = '"Access_Src"'  + "='" + "GAP - Default" + "'"
dm.MakeFeatureLayer(inFeatures, "TEMP_LYR")
dm.SelectLayerByAttribute ("TEMP_LYR", "NEW_SELECTION", expression)  

for i in range (0,63):
    with arcpy.da.UpdateCursor ("TEMP_LYR", fields) as cursor:
        for row in cursor:
            if row [0] == OA_list[i]:
                row [1] = "OA"
            elif row [0] == RA_list[i]:
                row [1] = "RA"
            elif row [0] == UK_list[i]:
                row [1] = "UK"
            elif row [0] == XA_list[i]:
                row [1] = "XA"
            cursor.updateRow(row)
del cursor

dm.Delete("TEMP_LYR")

For example, a feature with "Des_Tp" = 'NP" will be assigned an access of "OA". When I run the code, I encounter the error

Traceback (most recent call last):
  File "C:/Users/A/Desktop/Access_test.py", line 34, in <module>
    elif row [0] == UK_list[i]:
IndexError: list index out of range

I currently designate (0,63) as the range because that is how many variables I have in the "Des_Tp" field, but I may be interpreting this line incorrectly.

As Paul commented below, it might be possible to simplify the code with a dictionary. I've put them list in a dictionary, but the code runs without any change in the data:

destp_dict ={1:("NP","NM","NCA","NF","NG","PUB","NT","WSR","NRA","NLS","IRA","ACEC","REC","RMA","ACC","OCS","SP","SREC","SLHCA","SRMA","LP","LREC","LHCA","LRMA"),
2:("NWR","WA","WSA","NSBV","RNA","REA","HCA","SDA","SW","SCA","LCA"),
3:("MPA","PROC","FOTH","ND","TRIBL","SO","LO","UNK"),
4:("WPA","MIT","MIL","PCON","PREC","PHCA","PAGR","PRAN","PFOR","PO","PO","CONE","RECE","HCAE","HCAE","AGRE","RANE","FORE","OTHE","UNKE")}

fields = ['Des_Tp', 'Access']
access_type = ["OA", "RA", "UK", "XA"]

dm.MakeFeatureLayer(inFeatures, "TEMP_LYR")

with arcpy.da.UpdateCursor ("TEMP_LYR", fields) as cursor:
    for row in cursor:
        if row[0] == destp_dict[1]:
            row[1] = "OA"
        if row[0] == destp_dict[2]:
            row[1] = "RA"
        if row[0] == destp_dict[3]:
            row[1] = "UK"
        if row[0] == destp_dict[4]:
            row[1] = "XA"
        cursor.updateRow(row)

Update Cursor with Lists as Dictionary

I have a field, "Access" that I want to populate with 4 variables based on the attribute of another field, "Des_Tp".For example, if a feature has "Des_Tp" = 'NP', then I would want to code it as open access, 'OA' in the field "Access".

I've put the lists in a dictionary, but the code runs without any change in the data:

import arcpy
from arcpy import env

arcpy.env.overwriteOutput = True

inFeatures = r"C:\Users\A\AccessTest.gdb\Access_test"
destp_dict ={1:("NP","NM","NCA","NF","NG","PUB","NT","WSR","NRA","NLS","IRA","ACEC","REC","RMA","ACC","OCS","SP","SREC","SLHCA","SRMA","LP","LREC","LHCA","LRMA"),
2:("NWR","WA","WSA","NSBV","RNA","REA","HCA","SDA","SW","SCA","LCA"),
3:("MPA","PROC","FOTH","ND","TRIBL","SO","LO","UNK"),
4:("WPA","MIT","MIL","PCON","PREC","PHCA","PAGR","PRAN","PFOR","PO","PO","CONE","RECE","HCAE","HCAE","AGRE","RANE","FORE","OTHE","UNKE")}

fields = ['Des_Tp', 'Access']

arcpy.MakeFeatureLayer(inFeatures, "TEMP_LYR")

with arcpy.da.UpdateCursor ("TEMP_LYR", fields) as cursor:
    for row in cursor:
        if row[0] == destp_dict[1]:
            row[1] = "OA"
        if row[0] == destp_dict[2]:
            row[1] = "RA"
        if row[0] == destp_dict[3]:
            row[1] = "UK"
        if row[0] == destp_dict[4]:
            row[1] = "XA"
        cursor.updateRow(row)
added 14 characters in body
Source Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345

"Traceback (most recent call last): File "C:/Users/A/Desktop/Access_test.py", line 34, in elif row [0] == UK_list[i]: IndexError: list index out of range"

Traceback (most recent call last):
  File "C:/Users/A/Desktop/Access_test.py", line 34, in <module>
    elif row [0] == UK_list[i]:
IndexError: list index out of range

"Traceback (most recent call last): File "C:/Users/A/Desktop/Access_test.py", line 34, in elif row [0] == UK_list[i]: IndexError: list index out of range"

Traceback (most recent call last):
  File "C:/Users/A/Desktop/Access_test.py", line 34, in <module>
    elif row [0] == UK_list[i]:
IndexError: list index out of range
Post Closed as "Needs more focus" by PolyGeo
edited tags
Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345
Loading
added 1123 characters in body
Source Link
saoirse
  • 246
  • 2
  • 13
Loading
edited tags; edited title
Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345
Loading
Source Link
saoirse
  • 246
  • 2
  • 13
Loading