IndexError: list index out of range when using variable

89
4
Jump to solution
08-10-2022 06:38 AM
WhitneyWeber
Occasional Contributor

I am following the example within Editing Features: to select a feature within the feature layer.

sfo_feature = [f for f in ports_features if f.attributes['port_name']=='SAN FRANCISCO'][0]

The code is successful when hard-coding the value, as with 'SAN FRANCISCO' above, but fails with IndexError: list index out of range when using a variable. Following is my code:

The following is successful:

 

obs_feature = [f for f in obs_features if f.attributes['globalid']=='18d8c3ac-f828-4d93-b4e8-8a3b281f1790'][0]

 

And, the following is successful:

 

globalid = '18d8c3ac-f828-4d93-b4e8-8a3b281f1790'
obs_feature = [f for f in obs_features if f.attributes['globalid']==globalid][0]

 

The following fails, with IndexError: list index out of range when using a variable.  I know the value is correct via the use of AddMessage to know the value being queried:

 

globalid = "'"+original_globalid+"'"
arcpy.AddMessage("globalid: {}".format(globalid)+"; fld: {}".format(fld)+"; fld_value: {}".format(fld_value))
obs_feature = [f for f in obs_features if f.attributes['globalid']==globalid][0]

 

I assume this is something simple, but can't figure out the problem. I've tried format(globalid), although that shouldn't be necessary, but that fails as well.

Someone, please save me from myself!

0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Honored Contributor

Isn't original_globalid already a string with quotes? Are you putting an extra set of quotes around the string?

View solution in original post

4 Replies
KenBuja
MVP Honored Contributor

Isn't original_globalid already a string with quotes? Are you putting an extra set of quotes around the string?

WhitneyWeber
Occasional Contributor

Sorry, no, it's not already a string with quotes. . I should have put the result of the AddMessage:

globalid: '18d8c3ac-f828-4d93-b4e8-8a3b281f1790'; fld: EO_ID; fld_value: 300997.0

So, you can see the globalid from the AddMessage is the same as what was hardcoded in the code that works. It just doesn't work when using the variable.

0 Kudos
KenBuja
MVP Honored Contributor

However, note that the fld value is also a string and doesn't have quotes.

0 Kudos
WhitneyWeber
Occasional Contributor

You're a genius! I was obviously over-thinking this. Thank you so much! It always helps to have someone else's view.

0 Kudos