## HowTo:  Create sequential numbers in a field using Python in the Field Calculator

Article ID: 38517 ArcGIS - ArcEditor 10 ArcGIS - ArcInfo 10 ArcGIS - ArcView 10 N/A

#### Summary

Instructions provided describe how to create sequential numbers in a field using Python in the Field Calculator.

#### Procedure

The code in this article generates sequential numbers for unsorted data based on the OID or FID order. If your data is sorted on a field, the generated numbers will not be sequential.

1. Create a new short integer field.
2. Set the Parser to Python.
3. Select Show Codeblock.
4. Paste the following into the Pre-Logic Script Code:

```rec=0
def autoIncrement():
global rec
pStart = 1
pInterval = 1
if (rec == 0):
rec = pStart
else:
rec += pInterval
return rec```

5. Paste the following code in the smaller box below the Pre-Logic Script Code:

`autoIncrement()`

6. Click OK.

#### Related Information

Created: 12/3/2010

Article Rating: (14)

By agrati - 04/04/2013 3:28 AM

The article needs to be updated.

See our blog! http://oneteamgis.wordpress.com/2013/04/04/creare-un-contatore-numerico-crescente-sequential-numbers-in-un-campo-utilizzando-il-python-nel-the-field-calculator-attenti-a-come-scrivete-lo-script/

Rating:

By agrati - 04/04/2013 2:45 AM

The article needs to be updated.

The script must be rewritten in this way (the teeths are important): rec=0 def autoIncrement(): global rec pStart = 1 pInterval = 1 if (rec == 0): rec = pStart else: rec += pInterval return rec

Rating:

By vincenzo8808 - 04/02/2013 5:02 PM

Other - See details below.

Please Help. I am receiving error code 00059:Runtime error SyntaxError: 'return' outside function (,line10) when I attempt to run the following: rec=0 def autoIncrement(): global rec pStart = 1 pInterval = 1 if (rec == 0): rec = pStart else: rec += pInterval return rec

Rating:

By vincenzo8808 - 04/02/2013 4:58 PM

Other - See details below.

Rating:

By KTMduke - 03/25/2013 11:55 AM

Great article! It helped a lot!

How can I repeat this process with alphanumeric values?

Rating:

By Masare2 - 03/18/2013 4:57 AM

Great article! It helped a lot!

Rating:

By darrinchristy - 10/11/2012 4:59 AM

Other - See details below.

@VicMic - I just tried this with an Arcview license and it worked fine for me

Rating:

By VicMic - 09/10/2012 5:48 PM

The article is incorrect or the solution didn’t work.

The software section says that ArcView will do it but that is wrong. So to do this I have to: Exit out of Arcview Change my access in Administrator to Arcinfo Open Arcinfo Open my msd Do the Sort on my desired field Save the file to a new name Close the old file Do the code above Close my mxd Change my access in Administrator back to Arcview Open my mxd Set my thematic and labels again And then it still doesnâ€™t increment on my field but on the objectid field again. It seems that the Sort did not work. Coming from Mapinfo where this process took five seconds (sort by your field, type RowID in the update column box) this seems incredibly complicated and also very slow (working with 700 records). Why has the functionality gone from Arcview up to Arcinfo? [FID] + 1 did not work for me in Arcview or Arcinfo.

Rating:

By corea - 04/15/2012 4:33 PM

The article needs to be updated.

You need to clearly state that this works using the original order of the data (FID order) and not on the current sort order. So users have to first run the Arc Info SORT tool and then run this process if they want it ordered according to the current visible sort. It would be great if it can just do a increment based on the current order like in MapInfo and QGIS.

Rating:

By gorozcos - 11/30/2011 11:08 PM

Great article! It helped a lot!

Awesome! I did it without any problems! Thank you for your help!

Rating:

By rajesh6789 - 11/02/2011 11:55 PM

The article needs to be updated.

I want to generate sequence number continuously for adjacent polygon (not randomlly). This article is work but generated sequence number randomlly that i doesnt want.. Please provide me any other script that generated sequence number continuously for nearest parcel.

By mykotti - 10/25/2011 12:24 PM

Great article! It helped a lot!

I would like to see anothe article talks about how to assigna a unique value for each cell in the grid

Rating:

By jazmateta - 08/02/2011 11:10 AM

The article is incorrect or the solution didn’t work.

When I ran it, it gave the same number as the object id column. I need to be able to use another field that is auto incremented AFTER the table has been sorted on an address or such, not in the order in which the data was entered, so as to be able to put things in a LOGICAL order.

Rating:

By mcollinsamec - 07/05/2011 7:12 AM

Other - See details below.

This is a nice piece of code, but essentially the result is the same as [FID] +1

Rating:

By ssalas - 06/07/2011 2:17 PM

The article is incorrect or the solution didn’t work.

I attempted this with ArcMap 10sp1 against a personal geodatabase in an edit session. If more than 250 records were selected (I had 329), the resulting values started to repeat after assigning value 250.

Rating: