AnswerBun.com

Error using ArcPy SearchCursor to get list of unique values in multiple fields

Geographic Information Systems Asked by AlexS1 on November 7, 2020

I’m trying to extract a list of unique values in two fields in a shapefile. I think I am misunderstanding how the search cursor works, because I get the same error on which ever variable (x or y) I put second.

I’m following the answer here, and it works for just one field, but not 2+

Code:

with arcpy.da.SearchCursor(myshp, ['myid', 'myname']) as cursor:
    x = sorted({row[0] for row in cursor})
    y = sorted({row[1] for row in cursor})

Error:

y = sorted({row[1] for row in cursor})
IndexError: list index out of range

One Answer

You can use a generator expression for this. For example:

import arcpy

fc = r'C:pathtogeodatabase.gdbfeatureclass'

unique_list1 = set(row[0] for row in arcpy.da.SearchCursor(fc, 'myid'))
unique_list2 = set(row[0] for row in arcpy.da.SearchCursor(fc, 'myname'))

Correct answer by Aaron on November 7, 2020

Add your own answers!

Related Questions

Filtering Sentinel-5P from metadata on Google Earth Engine

1  Asked on February 22, 2021 by nabilla-dwi-chandra

       

OpenStreetMap baselayer of just one country

1  Asked on February 21, 2021 by rabie-ben

     

Creating table with data from raster to samples

2  Asked on February 21, 2021 by simone-castellana

   

Join CSV file to shapefile using gdal/ogr

3  Asked on February 21, 2021 by lincoln-mullen

         

Geodesic distance from raster in python?

1  Asked on February 21, 2021 by issy

         

Merging some polygons when creating Virtual Layer

1  Asked on February 21, 2021 by david-h-j

         

Saving to LAS file using laspy, correctly

1  Asked on February 21, 2021

   

PyQGIS standalone script using QGIS 3.2

2  Asked on February 21, 2021 by jessie-jes

       

Combine multiple shapefiles python

1  Asked on February 21, 2021

     

Write to GDAL raster to HDF5

1  Asked on February 20, 2021 by forrest-williams

       

Building walls to correct channelized flow in ArcHydro

1  Asked on February 20, 2021 by liz-gis

   

Ask a Question

Get help from others!

© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP