<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Split by Attribute in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/split-by-attribute/m-p/269846#M20778</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt; I was able to pull up a list of the unique store IDs by using:&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
def Unique_Values(table, field):
&amp;nbsp;&amp;nbsp;&amp;nbsp; with arcpy.da.SearchCursor(table, [field]) as cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return sorted({row[0] for row in cursor})

fc = "C:\Users\username\Documents\ArcGIS\Default.gdb\MyCustomers"
field = "DID"

UniqueValues (fc, field)
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;which resulted in a list of all of my unique store IDs, 957 in all.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Neat. I may steal that.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;What I'm not sure of however, is how to go about iterating through that list and using each value in the list in a where_clause in a "FeatureClassToFeatureClass_conversion" to export each unique dataset.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Note, you could avoid scripting altogether and do this in ModelBuilder using Iterate Feature Selection.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;But, if you want to do this in Python, it's a matter of making a layer and selecting on it:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
lyr = arcpy.CreateFeatureLayer(management(fc, "lyr")
stores = UniqueValues(lyr, field)
for store in stores:
&amp;nbsp;&amp;nbsp;&amp;nbsp; where = "{0} = {1}".format(field, store)
&amp;nbsp;&amp;nbsp;&amp;nbsp; outFC = "store{0}".format(store)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Select_analysis(lyr, outFC, where)
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;UPDATE: Chris beat me to the post. Nice one-liner, Chris!&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Just a note about my more verbose approach: If a GP tool gets a layer input, it can assume that the input exists and its field list is already in memory ready to roll. This can make a difference if you need to do 957 select/copy operations.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 13:12:12 GMT</pubDate>
    <dc:creator>curtvprice</dc:creator>
    <dc:date>2021-12-11T13:12:12Z</dc:date>
    <item>
      <title>Split by Attribute</title>
      <link>https://community.esri.com/t5/python-questions/split-by-attribute/m-p/269844#M20776</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have a Feature Class with 3.2 million records which represents customers. Each customer is assigned to a particular store ID (DID)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So I'm trying to split the customer record into store ID datasets. That is, for each store ID (DID), create a dataset of the customers assigned to it, essentially splitting the FC by each unique attribute in the field.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I was able to pull up a list of the unique store IDs by using:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;def Unique_Values(table, field): &amp;nbsp;&amp;nbsp;&amp;nbsp; with arcpy.da.SearchCursor(table, [field]) as cursor: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return sorted({row[0] for row in cursor})&amp;nbsp; fc = "C:\Users\username\Documents\ArcGIS\Default.gdb\MyCustomers" field = "DID"&amp;nbsp; UniqueValues (fc, field)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;which resulted in a list of all of my unique store IDs, 957 in all.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;What I'm not sure of however, is how to go about iterating through that list and using each value in the list in a where_clause in a "FeatureClassToFeatureClass_conversion" to export each unique dataset.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I know there are third party developed tools to do this, but they don't seem to work due to the number of records. I've tried every single one I could find.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 01 May 2013 17:19:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/split-by-attribute/m-p/269844#M20776</guid>
      <dc:creator>JohnDye</dc:creator>
      <dc:date>2013-05-01T17:19:04Z</dc:date>
    </item>
    <item>
      <title>Re: Split by Attribute</title>
      <link>https://community.esri.com/t5/python-questions/split-by-attribute/m-p/269845#M20777</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;How about something like:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;storeIdSet = set([r[0] for r in arcpy.da.SearchCursor(myFC, ["STORE_ID"])]) for storeId in storeIdSet: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outFC = r"C:\temp\test.gdb\store_" + str(storeId) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Select_analysis(myFC, outFC, "STORE_ID = " + str(storeId))&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 01 May 2013 17:39:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/split-by-attribute/m-p/269845#M20777</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2013-05-01T17:39:26Z</dc:date>
    </item>
    <item>
      <title>Re: Split by Attribute</title>
      <link>https://community.esri.com/t5/python-questions/split-by-attribute/m-p/269846#M20778</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt; I was able to pull up a list of the unique store IDs by using:&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
def Unique_Values(table, field):
&amp;nbsp;&amp;nbsp;&amp;nbsp; with arcpy.da.SearchCursor(table, [field]) as cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return sorted({row[0] for row in cursor})

fc = "C:\Users\username\Documents\ArcGIS\Default.gdb\MyCustomers"
field = "DID"

UniqueValues (fc, field)
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;which resulted in a list of all of my unique store IDs, 957 in all.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Neat. I may steal that.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;What I'm not sure of however, is how to go about iterating through that list and using each value in the list in a where_clause in a "FeatureClassToFeatureClass_conversion" to export each unique dataset.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Note, you could avoid scripting altogether and do this in ModelBuilder using Iterate Feature Selection.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;But, if you want to do this in Python, it's a matter of making a layer and selecting on it:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
lyr = arcpy.CreateFeatureLayer(management(fc, "lyr")
stores = UniqueValues(lyr, field)
for store in stores:
&amp;nbsp;&amp;nbsp;&amp;nbsp; where = "{0} = {1}".format(field, store)
&amp;nbsp;&amp;nbsp;&amp;nbsp; outFC = "store{0}".format(store)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Select_analysis(lyr, outFC, where)
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;UPDATE: Chris beat me to the post. Nice one-liner, Chris!&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Just a note about my more verbose approach: If a GP tool gets a layer input, it can assume that the input exists and its field list is already in memory ready to roll. This can make a difference if you need to do 957 select/copy operations.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:12:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/split-by-attribute/m-p/269846#M20778</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2021-12-11T13:12:12Z</dc:date>
    </item>
    <item>
      <title>Re: Split by Attribute</title>
      <link>https://community.esri.com/t5/python-questions/split-by-attribute/m-p/269847#M20779</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Chris,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I played with this notion earlier but couldn't get it working...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I modified your code a bit to export to a feature dataset and also provide print statements as it ran, &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;but all in all, right on the money.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks a ton!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It's probably going to take several hours to crunch through all the data but it seems to be chugging &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;along. The only issue I see is that since I'm running it in the Python Window, it keeps adding each dataset to my TOC. I didnt really expect that. It's not a huge deal because drawing is paused, but nonetheless, something I didnt expect to happen.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So long as it crunches through all the data, I'm going to take a stab at turning it into an Addin later.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;For anyone that wants the full code if you're having issues with large datasets, here's what I'm using:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy
from arcpy import env

# Create Workspace. 'Select_analysis' tool exports to the current workspace, so this is the directory 
# where your exported datasets will end up. I'm dropping mine in a Feature Dataset in the 'Default.gdb'
# on my system.
workspace = arcpy.env.workspace = "C:\Users\Username\My Documents\ArcGIS\Default.gdb\CustomersByStore"

# Create a variable to hold the FC you want to split by attribute.
fc = "C:\Users\Username\My Documents\ArcGIS\Default.gdb\Customers\MyCustomers"

# Instantiate a 'set' of unique values from the 'StoreID' field in the 'fc' dataset using the native 
# python 'set' class. Depending on how large your dataset is, this could take a bit of time.
# Mine was 3.2 million records and only took 3-4 minutes to run.
StoreSet = set([r[0] for r in arcpy.da.SearchCursor (fc, ["StoreID"])])

# Create the 'for' loop that will iterate through your BranchSet by unique ID.
for ID in BranchSet:
 # Create a variable to name each exported record according to the unique value. If your field is an 
 # integer, ensure you convert it to string. For some reason, just converting the integer to string
 # wasn't enough though, I had to also tack on a string ('Customers') as well before the conversion
 # to get it all working. By default, Select_analysis also exports to the current workspace, so if you
 # didn't set one as I did before, you need to hardcode the path here.
 OutFC = "Customers_" + str(DID)
 # Print which recordset the script is currently processing
 print "Exporting " + str(DID) + "..."
 # Export the dataset using the 'Select_analysis' function. Because my variable for 'OutFC' also 
 # holds the variable for my workspace, it drops all the datasets in my workspace. 
 arcpy.Select_analysis(fc, OutFC, "StoreID = " + str(DID))

print "Script Complete."
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:12:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/split-by-attribute/m-p/269847#M20779</guid>
      <dc:creator>JohnDye</dc:creator>
      <dc:date>2021-12-11T13:12:15Z</dc:date>
    </item>
    <item>
      <title>Re: Split by Attribute</title>
      <link>https://community.esri.com/t5/python-questions/split-by-attribute/m-p/269848#M20780</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;How about something like:&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;storeIdSet = set([r[0] for r in arcpy.da.SearchCursor(myFC, ["STORE_ID"])])
for storeId in storeIdSet:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outFC = r"C:\temp\test.gdb\store_" + str(storeId)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Select_analysis(myFC, outFC, "STORE_ID = " + str(storeId))&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Nice!&amp;nbsp; What a very pythonic way to write.&amp;nbsp; and I am stealing this &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;...so tough to remove the ArcObjects from my thinking!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:12:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/split-by-attribute/m-p/269848#M20780</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2021-12-11T13:12:17Z</dc:date>
    </item>
    <item>
      <title>Re: Split by Attribute</title>
      <link>https://community.esri.com/t5/python-questions/split-by-attribute/m-p/269849#M20781</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Neat. I may steal that.&lt;BR /&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Hey Curt,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I thought about doing a MakeFeatureLayer and then running a series of SelectLayerbyAttributes but from my experience, that significantly increases processing time. Not a big deal with small to moderate sized datasets, but with 3.2 million records, I didn't want to play with those implications.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Unfortunately, I can't take credit for the UniqueValues definition either, I lifted it off of an old &lt;/SPAN&gt;&lt;A href="http://arcpy.wordpress.com/2012/02/01/create-a-list-of-unique-field-values/"&gt;Cafe Python post&lt;/A&gt;&lt;SPAN&gt;. Someone commented on the post about leveraging the TableToNumPyArray function to get better performance, which made me really excited, but alas my feeble mind could not get it to work like I wanted.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 01 May 2013 19:00:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/split-by-attribute/m-p/269849#M20781</guid>
      <dc:creator>JohnDye</dc:creator>
      <dc:date>2013-05-01T19:00:00Z</dc:date>
    </item>
    <item>
      <title>Re: Split by Attribute</title>
      <link>https://community.esri.com/t5/python-questions/split-by-attribute/m-p/269850#M20782</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Hey Curt,&lt;BR /&gt;I thought about doing a MakeFeatureLayer and then running a series of SelectLayerbyAttributes but from my experience, that significantly increases processing time. Not a big deal with small to moderate sized datasets, but with 3.2 million records, I didn't want to play with those implications.&lt;BR /&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I agree - I fixed the code above so you're just selecting off the layer directly with the Select_analysis tool.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If you added a field index you'd get the time back in spades when you ran the Select tool -- even faster!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Someone commented on the post about leveraging the TableToNumPyArray function to get better performance&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Talk about gilding the lily. arcpy.da.SearchCursor is pretty darn fast. It's so fast, I wonder if Esri's team they used a numpy array to do it!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 01 May 2013 19:11:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/split-by-attribute/m-p/269850#M20782</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2013-05-01T19:11:04Z</dc:date>
    </item>
    <item>
      <title>Re: Split by Attribute</title>
      <link>https://community.esri.com/t5/python-questions/split-by-attribute/m-p/269851#M20783</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;&lt;BR /&gt;If you added a field index you'd get the time back in spades when you ran the Select tool -- even faster!&lt;BR /&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;That's a good idea, may play with that tomorrow!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 01 May 2013 19:15:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/split-by-attribute/m-p/269851#M20783</guid>
      <dc:creator>JohnDye</dc:creator>
      <dc:date>2013-05-01T19:15:52Z</dc:date>
    </item>
    <item>
      <title>ListUnique function</title>
      <link>https://community.esri.com/t5/python-questions/split-by-attribute/m-p/269852#M20784</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Here's my updated ListUnique script - works with 9x/10x.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks guys, this is a big improvement to what I had before.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;A cute tweak I learned that sorted(set()) returns a list.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
def ListUnique(inTable,Field):
&amp;nbsp;&amp;nbsp;&amp;nbsp; """Create a list of unique values from a table/tableview.

&amp;nbsp;&amp;nbsp;&amp;nbsp; arguments

&amp;nbsp;&amp;nbsp;&amp;nbsp; inTable&amp;nbsp; Table or table view
&amp;nbsp;&amp;nbsp;&amp;nbsp; Field&amp;nbsp;&amp;nbsp;&amp;nbsp; Field name
&amp;nbsp;&amp;nbsp;&amp;nbsp; """
&amp;nbsp;&amp;nbsp;&amp;nbsp; Row, Rows = None, None
&amp;nbsp;&amp;nbsp;&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # this will only work for 10.1
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; import arcpy.da
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lstValues = \
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sorted({r[0] for r in
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.da.SearchCursor(inTable, [Field])})
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; except:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; import arcgisscripting
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp = arcgisscripting.create(9.3)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Rows = gp.SearchCursor(inTable,"","",Field,Field)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Row = Rows.next()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lstValues = []
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; while Row:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lstValues.append(Row.getValue(Field))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Row = Rows.next()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # unique-ize and sort the list
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lstValues = sorted(set(lstValues))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return lstValues
&amp;nbsp;&amp;nbsp;&amp;nbsp; except:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; raise
&amp;nbsp;&amp;nbsp;&amp;nbsp; finally:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if Row: del Row
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if Rows: del Rows
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:12:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/split-by-attribute/m-p/269852#M20784</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2021-12-11T13:12:20Z</dc:date>
    </item>
    <item>
      <title>Re: Split by Attribute</title>
      <link>https://community.esri.com/t5/python-questions/split-by-attribute/m-p/269853#M20785</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;How about something like:&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;storeIdSet = set([r[0] for r in arcpy.da.SearchCursor(myFC, ["STORE_ID"])])
for storeId in storeIdSet:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outFC = r"C:\temp\test.gdb\store_" + str(storeId)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Select_analysis(myFC, outFC, "STORE_ID = " + str(storeId))&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Chris, I am getting an error message when I use this script indicating the SQL expression used in the select_analysis tool is invalid. I know how to set the expression to select a specific crime type, but I do not understand how to write an expression that will iterate through each crime type and make a new .shp file for each crime type.&amp;nbsp; How do you write a SQL expression that will iterate through values of a field?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This is how I have modified your code to fit my dataset:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;AllCrimes = "CopyCopySample2011_2013.shp"
CrimeTypeSet = set([r[0] for r in arcpy.da.SearchCursor(AllCrimes, ["CrimeType"])])
for CrimeType in CrimeTypeSet:
&amp;nbsp;&amp;nbsp;&amp;nbsp; out_FC = "H:\\PythonOutput\\" + str(CrimeType)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Select_analysis(AllCrimes, out_FC, "CrimeType = " + str(CrimeType)) &lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;the error message reads:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;"Executing: Select "H:\Sample Data\CopyCopySample2011_2013.shp" H:\PythonOutput\AgAssault.shp "CrimeType = AgAssault"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Start Time: Wed Apr 23 13:23:04 2014&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;ERROR 000358: Invalid expression CrimeType = AgAssault&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;A column was specified that does not exist.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;A column was specified that does not exist.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Failed to execute (Select).&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Failed at Wed Apr 23 13:23:05 2014 (Elapsed Time: 1.00 seconds)"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have tried so many things and I am about to throw my computer out the window!! What am I doing wrong?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:12:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/split-by-attribute/m-p/269853#M20785</guid>
      <dc:creator>AmberPerenzin</dc:creator>
      <dc:date>2021-12-11T13:12:23Z</dc:date>
    </item>
    <item>
      <title>Re: Split by Attribute</title>
      <link>https://community.esri.com/t5/python-questions/split-by-attribute/m-p/269854#M20786</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Have you tried this tool?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://resources.arcgis.com/gallery/file/geoprocessing/details?entryID=37AEB018-1422-2418-A036-CA6D9920F808"&gt;http://resources.arcgis.com/gallery/file/geoprocessing/details?entryID=37AEB018-1422-2418-A036-CA6D9920F808&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;And I imagine the problem with your query is the quotes. Try this using string substitution instead.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;"CrimeType = '{0}'".format(CrimeType)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 23 Apr 2014 16:46:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/split-by-attribute/m-p/269854#M20786</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2014-04-23T16:46:41Z</dc:date>
    </item>
    <item>
      <title>Re: Split by Attribute</title>
      <link>https://community.esri.com/t5/python-questions/split-by-attribute/m-p/269855#M20787</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Have you tried this tool?&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://resources.arcgis.com/gallery/file/geoprocessing/details?entryID=37AEB018-1422-2418-A036-CA6D9920F808"&gt;http://resources.arcgis.com/gallery/file/geoprocessing/details?entryID=37AEB018-1422-2418-A036-CA6D9920F808&lt;/A&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I have--but I need to use the Python script because this is just one of many steps I am programming &lt;span class="lia-unicode-emoji" title=":confused_face:"&gt;😕&lt;/span&gt; the python script for that specific tool is VERY long and complicated.&amp;nbsp; I am new to python, so the script that Chris posted is perfect for what I am doing.&amp;nbsp; I just cannot format the expression correctly.&amp;nbsp; Python is so frustrating!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The string substitution isn't working for me, but I could be entering it wrong.&amp;nbsp; I will keep trying &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; thank you for the suggestion!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 23 Apr 2014 16:49:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/split-by-attribute/m-p/269855#M20787</guid>
      <dc:creator>AmberPerenzin</dc:creator>
      <dc:date>2014-04-23T16:49:24Z</dc:date>
    </item>
    <item>
      <title>Re: Split by Attribute</title>
      <link>https://community.esri.com/t5/python-questions/split-by-attribute/m-p/269856#M20788</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Since "crime type" is probably a string field, you just need single quotes.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So instead of this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;storeIdSet = set([r[0] for r in arcpy.da.SearchCursor(myFC, ["STORE_ID"])])
for storeId in storeIdSet:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outFC = r"C:\temp\test.gdb\store_" + str(storeId)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Select_analysis(myFC, outFC, "STORE_ID = " + str(storeId))&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Do this (difference is in the last line):&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;storeIdSet = set([r[0] for r in arcpy.da.SearchCursor(myFC, ["STORE_ID"])])
for storeId in storeIdSet:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outFC = r"C:\temp\test.gdb\store_" + str(storeId)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Select_analysis(myFC, outFC, "STORE_ID = '" + str(storeId) +"'")&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Mzcoyle formatting stuff also will work... Basically I think you just need to edit the SQL expression so it will deal with string values instead of numbers...&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:12:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/split-by-attribute/m-p/269856#M20788</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2021-12-11T13:12:25Z</dc:date>
    </item>
    <item>
      <title>Re: Split by Attribute</title>
      <link>https://community.esri.com/t5/python-questions/split-by-attribute/m-p/269857#M20789</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Make sure "CrimeType" is a field that actually exists in CopyCopySample2011_2013.shp.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Also: "CopyCopy"????&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 23 Apr 2014 17:06:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/split-by-attribute/m-p/269857#M20789</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2014-04-23T17:06:10Z</dc:date>
    </item>
    <item>
      <title>Re: Split by Attribute</title>
      <link>https://community.esri.com/t5/python-questions/split-by-attribute/m-p/269858#M20790</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;It could also be you need delimiters on your field, though it should be unnecessary.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;AllCrimes = "CopyCopySample2011_2013.shp"
CrimeTypeSet = set([r[0] for r in arcpy.da.SearchCursor(AllCrimes, ["CrimeType"])])
for CrimeType in CrimeTypeSet:
&amp;nbsp;&amp;nbsp;&amp;nbsp; out_FC = "H:\\PythonOutput\\" + str(CrimeType)
&amp;nbsp;&amp;nbsp;&amp;nbsp; query = """{0} = '{1}'""".format(arcpy.AddFieldDelimiters(AllCrimes, "CrimeType"), CrimeType)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Select_analysis(AllCrimes, out_FC, query)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:12:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/split-by-attribute/m-p/269858#M20790</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2021-12-11T13:12:28Z</dc:date>
    </item>
    <item>
      <title>Re: Split by Attribute</title>
      <link>https://community.esri.com/t5/python-questions/split-by-attribute/m-p/269859#M20791</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;SUCCESS!!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you both!&amp;nbsp; When I entered the single quotes everything worked!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;mzcoyle: ref "copy copy" Since I am new to python I know I am going to screw up my dataset.&amp;nbsp; Making one backup wasn't enough, I had to backup the backup &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you again! I was stuck on this for longer than I would like to admit (days, not hours...ugh).&amp;nbsp; You are a lifesaver!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 23 Apr 2014 17:12:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/split-by-attribute/m-p/269859#M20791</guid>
      <dc:creator>AmberPerenzin</dc:creator>
      <dc:date>2014-04-23T17:12:48Z</dc:date>
    </item>
  </channel>
</rss>

