HOW TO

Find the missing sequence number of a field in ArcGIS Pro using ArcPy

Last Published: May 6, 2024

Summary

In ArcGIS Pro, some data may contain sequential numbers for maintaining data integrity, ensuring data quality, and supporting spatial analysis. In some cases, there are missing sequence numbers in the data, which can cause errors potentially leading to inconsistencies or inaccuracies in analysis, as shown in the image below.

Missing sequential numbers

This article describes the workflow to find the missing sequence number of a field in ArcGIS Pro using ArcPy.

Procedure

Note:
This workflow requires a full script to run in the ArcGIS Pro Python window. All the indents must be retained as portrayed in the code block.
  1. In ArcGIS Pro, open the map containing the feature layer with the sequential numbers.
  2. Open the Python window. Refer to ArcGIS Pro: Python window for more information.
  3. Run the following script.
    1. Initialize a list to store the existing sequence numbers.
import arcpy

def find_missing_sequence(fc, field_name):
    sequence_numbers = []
  1. Create a search cursor to iterate through the feature class and convert the field value to an integer.
    with arcpy.da.SearchCursor(fc, [field_name]) as cursor:
        for row in cursor:
            # Convert the field value to an integer (assuming it's a float)
            sequence_numbers.append(int(row[0]))
  1. Find the minimum and maximum sequence numbers.
    min_seq = min(sequence_numbers)
    max_seq = max(sequence_numbers)
  1. Create a list of all the expected sequence numbers.
    expected_sequence = list(range(min_seq, max_seq + 1))
  1. Find the missing sequence numbers.
    missing_sequence = [num for num in expected_sequence if num not in sequence_numbers]

    return missing_sequence
  1. Set the feature class and field name. Replace <featureClassName> with the name of the feature class. Replace <fieldName> with the name of the field.
if __name__ == "__main__":
    fc = "<featureClassName>"
    field_name = "<fieldName>"
  1. Call the function to find and print the missing sequence numbers.
    missing_sequence = find_missing_sequence(fc, field_name)

    if missing_sequence:
        print("Missing sequence number(s):", missing_sequence)
    else:
        print("No missing sequence numbers found.")

The code block below demonstrates the full working script.

import arcpy

def find_missing_sequence(fc, field_name):
    sequence_numbers = []

    with arcpy.da.SearchCursor(fc, [field_name]) as cursor:
        for row in cursor:
            sequence_numbers.append(int(row[0]))

    min_seq = min(sequence_numbers)
    max_seq = max(sequence_numbers)

    expected_sequence = list(range(min_seq, max_seq + 1))

    missing_sequence = [num for num in expected_sequence if num not in sequence_numbers]

    return missing_sequence

if __name__ == "__main__":
    fc = "Point1"
    field_name = "Sequence"

    missing_sequence = find_missing_sequence(fc, field_name)

    if missing_sequence:
        print("Missing sequence number(s):", missing_sequence)
    else:
        print("No missing sequence numbers found.")

The image below shows the missing sequence number in the field using ArcPy.

The missing sequence number in a field

Article ID: 000032437

Software:
  • ArcGIS Pro 3 1
  • ArcGIS Pro 3 0
  • ArcGIS Pro 3 2

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options