# Python: retrieving from list & adding autoincrement IDs

Question asked by hsmall on Jul 14, 2014
Latest reply on Jul 17, 2014 by TomGoeke

Hi all,

I was wondering if you could help me write a script for Field Calculator.

I have a list of 2,000+ entries contained in about 40 parks. Each park has a field ParkID which is already filled out. I'd like to create a script that generates UniqueIDs based on the ParkIDs.

Ex. ParkID = Q001

.:. UniqueIDs = Q001_001, Q001_002, all the way to Q001_100 or however many necessary depending on how many entries, but then beginning at 001 again when reaching a new park, i.e. R142_001, R142_002, etc.

I want to first retrieve each ParkID from the list of ParkIDs and then use that list to create an autoincrement starting at 001 for each unique ParkID.

I'd rather do this that just do an autoincrement for each park individually.

I have very little python experience and might not quite understand some details, so could you please explain in depth?

EDIT:
Right now I have the following script in the Codeback of Field Calculator:

rec=0

plot = ""

def autoIncrement(park):

global rec

global plot

pStart = 1

pInterval = 1

if (rec == 0):

rec = pStart

plot = park

plot = plot + "_000"+ str(rec)

rec += pInterval

elif (rec < 10):

plot = park + "_000" + str(rec)

rec += pInterval

elif (rec > 9 and rec < 100):

plot = park + "_00" + str(rec)

rec += pInterval

elif (rec > 99 and rec < 1000):

plot = park + "_0" + str(rec)

rec += pInterval

else:

plot = park +  "_" + str(rec)

rec += pInterval

return plot

And the following in the "FIELD_NAME" = autoIncremet(!ParkID!)

This code works, but starts the unique ID at 0 and goes up into the thousands. I am trying to find a way to make it so for each new ParkID, the unique ID restarts at 001 again.

Best,

Hayley

Message was edited by: Hayley Small