I want to query a feature class on attribute 'state' == 'new', and if so, add that feature class record to a list (I will process this list later).

I have:

cursor = arcpy.da.SearchCursor(points, 'state')
for row in cursor:
    if row[0] == 'new':

('points' is the feature class, 'state' is the attribute within the feature class) but that just makes a list of tuples, not feature class records!

I ultimately want a list of records in that feature class with a status of 'new', so that I can loop through them and perform other functions.

  • SearchCursor accepts a where_clause parameter, so you don't even need to test the row[0] value. You'll still have the issue of only saving the field with "new" in it. – Vince Jan 6 at 18:29
  • What is your next processing step? – BERA Jan 6 at 18:35
  • I need to take this list of new points, find a way to relate them to each other (no keys, can only use timestamp or spatial distance apart and user that created them), then relate them to the nearest linear feature and snap them to that feature. Basically, the points are breadcrumb points along a line and I need to snap to that line and segment that line, then mark that section of the line as 'inspected'-I know, a lot of work! – m.Walker Jan 6 at 19:54

new_point_list.append(row[0]). But im guessing you want to append objectids (or some other unique identifier) instead, if you want to do more processing later.

If so you can try something like this:

oids = []
with arcpy.da.SearchCursor(points, ['state','OID@']) as cursor:
    for row in cursor:
        if row[0] == 'new':
  • 1
    Best done within a with or using a del, no? – Vince Jan 6 at 18:33
  • If the source is file geodatabase, there won't be a huge time difference with where_clause="state = 'new'" but if RDBMS or lots of rows, it could. – Vince Jan 6 at 18:56
  • But this only gets me a list of ints, not actual records. Is there not a way to just build a table or records, and work with the records directly (instead of re-querying on OID to perform additional steps, for which I have to compare various other attributes and gets complicated)? – m.Walker Jan 6 at 19:51
  • 1
    Use Select tool, or make feature layer tool. No need for cursor – BERA Jan 6 at 19:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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