# Populate rows using Update Cursor based on a list of repeating values

789
3
09-09-2016 08:47 AM
Occasional Contributor III

I've been searching high and low for this (Stackoverflow/exchange, etc, Geonet...). Here it goes. I have a table with a number of records. I have a field called 'Rotation' I would like to populate with a repeating set of values [0,90,180,270]. So imagine the following:

PT_IDROTATION

1

0
290
3180
4270
50
690
7180
8270
90

I've tried to simplify my question as much as possible, but its literally as simple as that. Most of the searches return information related to deleting duplicate/repeated records & values.

Tags (3)
1 Solution

Accepted Solutions
MVP Esteemed Contributor

itertools.cycle()

Haven't tested but something like:

``````>>> from itertools import cycle
>>> rot = cycle((0,90,180,270))
>>> with arcpy.da.UpdateCursor(in_table,["PT_ID","ROTATION"]) as cur:
...     for row in cur:
...         row[1] = next(rot)
...         cur.updateRow(row)‍‍‍‍‍‍``````
3 Replies
MVP Esteemed Contributor

itertools.cycle()

Haven't tested but something like:

``````>>> from itertools import cycle
>>> rot = cycle((0,90,180,270))
>>> with arcpy.da.UpdateCursor(in_table,["PT_ID","ROTATION"]) as cur:
...     for row in cur:
...         row[1] = next(rot)
...         cur.updateRow(row)‍‍‍‍‍‍``````
Occasional Contributor III

Joshua, this is exactly what I needed, thanks!!!

MVP Honored Contributor

Go with Joshua's answer, I was just curious to see how hard this would be in Field Calculator (as long as your IDs are reliable):

VB Script (but could do the same in Python): (([PT_ID]-1)*90)-( Int(([PT_ID]/4)-0.25) *360)