Can anyone tell me if there is a way to go up one row in python cursor?

i.e. opposite of row = rows.next()

  • 3
    What are you trying to do? I don't think you can move back with a cursor they are forward only – Nathan W Jun 14 '13 at 4:55
  • Why not just use the sort_fields argument (arcpy) or the sql_clause argument with an ORDER BY clause (arcpy.da)? – blah238 Jun 14 '13 at 11:12

Most cursors don't support iterating backward, but there's an easy way to get the previous row's value, just store a variable:

previous_row = None
for row in rows.next():
    # do stuff here
    # now, store the value of the current row into previous row before
    # the next iteration is started
    previous_row = row

If you have a need to reset the cursor, many support a reset operation. If you're using ArcGIS, this is .reset().

Update: Arash asked how to get values that are even earlier in the dataset. One way would be to store multiple previous rows, extending the logic above. If the dataset is small, another approach would be to store the values already seen in a list of tuples, and index the cursor. The code above does this based on the specific question asked:

previous_data = []
for (i, row) in enumerate(rows.next()):
     x = row[0]
     y = row[1]
     # add this record to our 'seen' data:
     previous_data.append((x, y))
     # now, if we need a previous point, we can ask for it by index 
     if i >= 2:
         (n, m) = previous_data[i-2]
         # now n and m store the values seen two records previously.
|improve this answer|||||
  • Shaun, Thank you. This is a good point but I am not sure if it helps me or not. What I want to do is that imagine we have 2 fields X and Y, we add two more fields M and N. I want to assign X1 (row 1 under field X) to M3 and Y1 to N3, X2 to M4 and Y2 to N4, X3 to M5 and Y3 to N5, ... Any thought? – Arash Jun 15 '13 at 1:47
  • I've updated my response to include details on this more specific question. – scw Jun 15 '13 at 7:22
  • Shaun, thank you for teaching me new points! I appreciate it. – Arash Jun 16 '13 at 19:57

What about putting row in the list and iterating backwards?

EDIT: Turns out, that rows object does not work this way :) and row in the list lost reference. But depending on your needs, you may put on the list row.whatever_you_may_need and then iterate backwards. Of course this may work only for SearchCursor and only for some use cases. Also you may check how the rows.next() is iterating over the rows container, if it depends on fetures FID or something else, you may try to revers feature order in FC (also may be option for some caseses).

|improve this answer|||||
  • Your code is wrong which is why you might be lossing the reference. You don't need the row = rows.next() stuff if you are using a for loop. You could also just do: list(rows) which would make it into a list – Nathan W Jun 14 '13 at 23:34
  • Tomek/ Nathan, thank you! need more time to test the idea... – Arash Jun 15 '13 at 1:48
  • Yeah, it should be while loop instead. Anyway, it does not matter, since this does not explain lost reference. Actually it got reference to 1st item from the rows object and stopped, resulting in list of 29 items with values from 1st. It was just loose suggestion, to try this approach, but it does not work. I'll leave answer anyway, for info. Just remove code. – Tomek Jun 15 '13 at 8:15

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.