<?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: Get Count returning wrong specified selected feature class in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/get-count-returning-wrong-specified-selected/m-p/1617571#M74248</link>
    <description>&lt;LI-CODE lang="python"&gt;def mapping(s, t, u, r):
    lot_site = dict()
    sale_site = dict()

    parcel_site =[]

    spatial_ref = arcpy.Describe(u).spatialReference

    for row in arcpy.da.SearchCursor(u, ['CountyPIN', 'SHAPE@X', 'SHAPE@Y']):
        if row[0] in t:
            lot_site.update({row[0] : (row[1], row[2])})

    for row in arcpy.da.SearchCursor(r,['PIN','SHAPE@X', 'SHAPE@Y']):
        if row[0] not in lot_site and row[0] in t:
            lot_site.update({row[0] : (row[1], row[2])})

    fc_feedlots = arcpy.CreateFeatureclass_management('in_memory', 'FeedPoint', 'POINT', '', '', '', spatial_ref)
    arcpy.AddField_management(fc_feedlots, 'PIN', 'TEXT')

    with arcpy.da.InsertCursor(fc_feedlots, ['SHAPE@', 'PIN']) as cur:
        for k, v in lot_site.iteritems():
            feed_point = [v[0], v[1]]
            cur.insertRow([feed_point, k])

    feedPoint_lyr = arcpy.MakeFeatureLayer_management(fc_feedlots, 'feedlot_lyr')
    address_lyr = arcpy.MakeFeatureLayer_management(u, 'Address_lyr')
    x = 0
    for row in arcpy.da.SearchCursor(feedPoint_lyr, ['SHAPE@', 'PIN']):
        select_feedlot = arcpy.SelectLayerByAttribute_management(feedPoint_lyr, "NEW_SELECTION", "PIN = '{}'".format(row[0]))
        sales = arcpy.management.SelectLayerByLocation(
                address_lyr, 'WITHIN_A_DISTANCE',
                row[0], '2640 FEET'
                )
        for sale in arcpy.da.SearchCursor(sales, ['CountyPIN']):
            if sale[0] in s:
                x += 1
                #sales_check(sale[0], sale_site)
                saleParcel = sale[0]
                feedParcel = row[0]
                feedPerm = t.get(row[0])[0]
                adj = s.get(sale[0])[0]
                netsale = s.get(sale[0])[1]
                pt = s.get(sale[0])[2]
                au_state = t.get(row[0])[1]
                ratio = s.get(sale[0])[3]
                count = int(arcpy.management.GetCount(feedPoint_lyr)[0])
            
                sale_site.update({x : [saleParcel, feedParcel, feedPerm, adj, netsale, pt, au_state, ratio, count]})
            
            
          

    arcpy.Delete_management('in_memory')
    print("Sale_site dictionary has been updated using Feedlots and Sales info")
    return sale_site&lt;/LI-CODE&gt;&lt;P&gt;My apologies! I didn't know that this was an option, I hope this is helpful!!&lt;/P&gt;</description>
    <pubDate>Thu, 22 May 2025 15:25:32 GMT</pubDate>
    <dc:creator>GISIntern21</dc:creator>
    <dc:date>2025-05-22T15:25:32Z</dc:date>
    <item>
      <title>Get Count returning wrong specified selected feature class</title>
      <link>https://community.esri.com/t5/python-questions/get-count-returning-wrong-specified-selected/m-p/1616438#M74217</link>
      <description>&lt;P&gt;Hello! I am working on a Python project, and I am just about done with it, but I am struggling with formatting my Get Count function. I am writing a script to get the number of sales within a 1/2 mile of feedlots, but to finish it off, I need to return the number of feedlots that are within that half mile of the sales. So if a particular sale point has 1-3 feedlots within a half mile, I need to return that number, but right now, I am getting the number of sales instead (basically the opposite of what I need). I have tried swapping the feature classes in my function, but I only get '1' as a result. Is there a better way to write my code, or am I missing something?&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="GISIntern21_0-1747747201722.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/132635iFB06A605AC0433B2/image-size/medium?v=v2&amp;amp;px=400" role="button" title="GISIntern21_0-1747747201722.png" alt="GISIntern21_0-1747747201722.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 May 2025 13:20:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/get-count-returning-wrong-specified-selected/m-p/1616438#M74217</guid>
      <dc:creator>GISIntern21</dc:creator>
      <dc:date>2025-05-20T13:20:08Z</dc:date>
    </item>
    <item>
      <title>Re: Get Count returning wrong specified selected feature class</title>
      <link>https://community.esri.com/t5/python-questions/get-count-returning-wrong-specified-selected/m-p/1616450#M74220</link>
      <description>&lt;P&gt;I think there's an order of operations problem here.&lt;/P&gt;&lt;P&gt;First thing: I don't think "select_feedlot" is necessary; you don't use it and also it's redundant for what you're doing anyway.&lt;/P&gt;&lt;P&gt;The way a cursor works is by going through each record in a table, one at a time. So, doing a select by attribute on the same table to get the exact same record is not a good use of time.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is also the cause of the other, larger, problem, which is that all of your select by locations are plugging in the entire feedPoint_lyr, not just the current record you're trying to analyze for. I'm assuming that all of your sales are within 2640 feet of&amp;nbsp;&lt;STRONG&gt;&lt;EM&gt;SOME&lt;/EM&gt;&lt;/STRONG&gt; feedlot, so if you plug in every feedlot, you're going to get every sale.&lt;/P&gt;&lt;P&gt;To get around this, use the SHAPE@ keyword (not sure if that's the right term for it) in your cursor's fields, and feed each record's geometry to SelectByLocation&lt;/P&gt;&lt;LI-CODE lang="python"&gt;with arcpy.da.SearchCursor(feedPoint_lyr, ['PIN', 'SHAPE@']) as cursor:
    for row in cursor:
        # Each row is returned as a tuple: (PIN, geometry)
        # feed it the second item in the tuple
        sales = arcpy.management.SelectLayerByLocation(
                            address_lyr, 'WITHIN_A_DISTANCE', 
                            row[1], '2640 FEET'
                            )&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 May 2025 13:47:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/get-count-returning-wrong-specified-selected/m-p/1616450#M74220</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2025-05-20T13:47:32Z</dc:date>
    </item>
    <item>
      <title>Re: Get Count returning wrong specified selected feature class</title>
      <link>https://community.esri.com/t5/python-questions/get-count-returning-wrong-specified-selected/m-p/1616458#M74221</link>
      <description>&lt;P&gt;Alrighty! I'm still fairly new to Python, so I figured it was most likely an order of operations problem..&amp;nbsp;&lt;BR /&gt;For formatting purposes, the code sample you shared, would that just substitute my 'for row in arcpy though the selections? My end goal is to create a csv output that has the sale and feedlot parcels, permits of the feedlots, etc, so I'm wanting to try and loop through all of my records I believe.&lt;/P&gt;</description>
      <pubDate>Tue, 20 May 2025 14:10:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/get-count-returning-wrong-specified-selected/m-p/1616458#M74221</guid>
      <dc:creator>GISIntern21</dc:creator>
      <dc:date>2025-05-20T14:10:50Z</dc:date>
    </item>
    <item>
      <title>Re: Get Count returning wrong specified selected feature class</title>
      <link>https://community.esri.com/t5/python-questions/get-count-returning-wrong-specified-selected/m-p/1616462#M74222</link>
      <description>&lt;P&gt;No problem!&lt;/P&gt;&lt;P&gt;The way you have it set up isn't wrong.&lt;/P&gt;&lt;P&gt;Cursors are conventionally done the way I did it (peep the &lt;A href="https://pro.arcgis.com/en/pro-app/latest/arcpy/data-access/searchcursor-class.htm" target="_blank" rel="noopener"&gt;documentation for arcpy.da.SearchCursor&lt;/A&gt;, although the documentation for the older &lt;A href="https://pro.arcgis.com/en/pro-app/latest/arcpy/functions/searchcursor.htm" target="_blank" rel="noopener"&gt;arcpy.SearchCursor&lt;/A&gt; doesn't do it) because it's supposed to be used as a&amp;nbsp;&lt;A href="https://www.geeksforgeeks.org/context-manager-in-python/" target="_blank" rel="noopener"&gt;context manager&lt;/A&gt;, meaning that it closes itself when it's done, removing any locks and freeing up resources. This sort of construction is great in case of errors; if you use it to open a file and then something breaks, it closes the file as part of the error reporting process.&lt;/P&gt;&lt;P&gt;That being said, it's been &lt;A href="https://community.esri.com/t5/python-ideas/add-context-manager-methods-to-arcpy-cursor/idi-p/1334553" target="_blank" rel="noopener"&gt;discovered&lt;/A&gt; that cursors don't actually behave like this, at least &lt;A href="https://www.esri.com/arcgis-blog/products/arcgis-pro/data-management/locked-by-another-application-using-arcpy-and-a-file-geodatabase" target="_blank" rel="noopener"&gt;in file geodatabases&lt;/A&gt;, so you should still add in a &lt;STRONG&gt;del&lt;/STRONG&gt; when you're done as a best practice (even though it's supposed to be doing it behind the scenes for you automatically when you use a &lt;STRONG&gt;with.&lt;/STRONG&gt;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;with arcpy.da.SearchCursor(lay, fields) as cursor:
    for row in cursor:
        #code
        pass
del cursor&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 May 2025 14:26:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/get-count-returning-wrong-specified-selected/m-p/1616462#M74222</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2025-05-20T14:26:06Z</dc:date>
    </item>
    <item>
      <title>Re: Get Count returning wrong specified selected feature class</title>
      <link>https://community.esri.com/t5/python-questions/get-count-returning-wrong-specified-selected/m-p/1616699#M74225</link>
      <description>&lt;P&gt;Alfred's covered almost all of the basics you need here really well.&lt;/P&gt;&lt;P&gt;One thing I feel is important to explicitly point out where his code meaningfully differs from yours is that he has only&amp;nbsp;&lt;U&gt;&lt;STRONG&gt;one&lt;/STRONG&gt;&lt;/U&gt; level of SearchCursor.&amp;nbsp; This is very deliberate, and decidedly a best practice.&lt;/P&gt;&lt;P&gt;Nesting different SearchCursors or UpdateCursors can cause your code to be monumentally slower than you'd expect, because the &lt;STRONG&gt;inner cursor runs through its entire table/view &lt;U&gt;once for&amp;nbsp;every&amp;nbsp;record&lt;/U&gt; in the outer cursor.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Simply put, consider the following:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;with arcpy.da.SearchCursor(tbl_one, fieldList_one) as cursor1:
    for rowA in cursor1:
        with arcpy.da.SearchCursor(tbl_two, fieldList_two) as cursor2:
            for rowB in cursor2:
                #do stuff&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you have 100 records in&amp;nbsp;&lt;STRONG&gt;tbl_one&lt;/STRONG&gt; and 100 records in&amp;nbsp;&lt;STRONG&gt;tbl_two&lt;/STRONG&gt;, you'd end up in a loop of 10,000 iterations.&amp;nbsp; This problem also grows exponentially, as you can probably see.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Even working with a smaller inner table like you were, this is often incredibly inefficient.&amp;nbsp; There are very narrow cases where it&amp;nbsp;&lt;EM&gt;might&lt;/EM&gt; appear okay, at first glance.&amp;nbsp; But I would argue that it's almost always a red flag that your underlying/theoretical procedure needs some revisiting.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The one exception typically cited is InsertCursor.&amp;nbsp; Because of the way that one works, it's both fine and frequently standard practice to nest it inside either a SearchCursor or UpdateCursor.&lt;/P&gt;</description>
      <pubDate>Wed, 21 May 2025 00:44:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/get-count-returning-wrong-specified-selected/m-p/1616699#M74225</guid>
      <dc:creator>MErikReedAugusta</dc:creator>
      <dc:date>2025-05-21T00:44:47Z</dc:date>
    </item>
    <item>
      <title>Re: Get Count returning wrong specified selected feature class</title>
      <link>https://community.esri.com/t5/python-questions/get-count-returning-wrong-specified-selected/m-p/1617132#M74237</link>
      <description>&lt;P&gt;If you want a really compact way to do this, you can build mappings between features really easily using SearchCursors and dictionary comprehensions:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from typing import TypeAlias
from arcpy import LinearUnitConversionFactor
from arcpy.da import SearchCursor

feedPoint_lyr = "feedPoint"
address_lyr = "address"
feature_units = 'Meters' # Replace with your feature units
_MILES = LinearUnitConversionFactor(feature_units, "Miles")
# Half Mile in in feature units
BUFFER = 0.5*_MILES

# Change this to the datatype of your PIN fields
PinType: TypeAlias = int
#PinType: TypeAlias = str

sale_to_lot: dict[PinType, list[PinType]] = {
    # Find all lots within `BUFFER` of each address
    pin: list(SearchCursor(feedPoint_lyr, ['PIN'], spatial_filter=point.buffer(BUFFER)))
    # Iterate all addresses
    for pin, point in SearchCursor(address_lyr, ['CountyPIN', 'SHAPE@'])
}

lot_to_sale: dict[PinType, list[PinType]] = {
    # Find all addresses within `BUFFER` of each feedlot
    pin: list(SearchCursor(address_lyr, ['CountyPIN'], spatial_filter=point.buffer(BUFFER)))
    # Iterate all feed lots
    for pin, point in SearchCursor(feedPoint_lyr, ['PIN', 'SHAPE@'])
}&lt;/LI-CODE&gt;&lt;P&gt;This will give you two mappings, one mapping each feedlot to the addresses within 0.5 miles and one mapping addresses to lots within 0.5 miles.&lt;/P&gt;&lt;P&gt;You can then use these PIN mappings however you want without having to do a spatial query again.&lt;/P&gt;&lt;P&gt;This solution is more memory efficient, but spools up an ungodly amount of search cursors (len(address)+len(feedlots)). You can avoid this by loading the features into memory first:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from typing import TypeAlias
from arcpy import LinearUnitConversionFactor, PointGeometry
from arcpy.da import SearchCursor

feedPoint_lyr = "feedPoint"
address_lyr = "address"
feature_units = 'Meters' # Replace with your feature units
_MILES = LinearUnitConversionFactor(feature_units, "Miles")
# Half Mile in in feature units
BUFFER = 0.5*_MILES

# Change this to the datatype of your PIN fields
PinType: TypeAlias = int
#PinType: TypeAlias = str

# Load addresses
addresses: dict[PinType, PointGeometry] = {
    pin: shape
    for pin, shape in SearchCursor(address_lyr, ['CountyPIN', 'SHAPE@'])
}

# Load feedlots
feedlots: dict[PinType, PointGeometry] = {
    pin: shape
    for pin, shape in SearchCursor(feedPoint_lyr, ['PIN', 'SHAPE@'])
}


sale_to_lot: dict[PinType, list[PinType]] = {}
for address_pin, address_shape in addresses.items():
    sale_to_lot[address_pin] = [
        lot_pin
        for lot_pin, lot_shape in feedlots.items()
        # Skip lot if it is not within `BUFFER`
        if not lot_shape.disjoint(address_shape.buffer(BUFFER))
    ]

lot_to_sale: dict[PinType, list[PinType]] = {}
for lot_pin, lot_shape in feedlots.items():
    lot_to_sale[lot_pin] = [
        address_pin
        for address_pin, address_shape in addresses.items()
        # Skip address if it is not within `BUFFER`
        if not address_shape.disjoint(lot_shape.buffer(BUFFER))
    ]&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 21 May 2025 17:45:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/get-count-returning-wrong-specified-selected/m-p/1617132#M74237</guid>
      <dc:creator>HaydenWelch</dc:creator>
      <dc:date>2025-05-21T17:45:52Z</dc:date>
    </item>
    <item>
      <title>Re: Get Count returning wrong specified selected feature class</title>
      <link>https://community.esri.com/t5/python-questions/get-count-returning-wrong-specified-selected/m-p/1617544#M74244</link>
      <description>&lt;P&gt;That makes sense!&amp;nbsp;&lt;BR /&gt;I'm still running into some issues with getting it to work, as well as an AttributeError: _exit_ issue.. Here is what the bulk of my script looks like for a little bit more context...&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="GISIntern21_0-1747924578980.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/132979i41A03D2E1D50884F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="GISIntern21_0-1747924578980.png" alt="GISIntern21_0-1747924578980.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;For the lot_site dict and sale_site dict, I haven't had any issues with how they are set up or anything of the sorts. My script has been running very well other than when I try and troubleshoot my GetCount objectives.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 22 May 2025 14:37:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/get-count-returning-wrong-specified-selected/m-p/1617544#M74244</guid>
      <dc:creator>GISIntern21</dc:creator>
      <dc:date>2025-05-22T14:37:56Z</dc:date>
    </item>
    <item>
      <title>Re: Get Count returning wrong specified selected feature class</title>
      <link>https://community.esri.com/t5/python-questions/get-count-returning-wrong-specified-selected/m-p/1617565#M74247</link>
      <description>&lt;P&gt;Um, would it be possible for you to post it as a codeblock?&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.esri.com/t5/python-blog/code-formatting-the-community-version/ba-p/1007633" target="_blank" rel="noopener"&gt;(1) Code formatting ... the Community Version - Esri Community&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 22 May 2025 15:15:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/get-count-returning-wrong-specified-selected/m-p/1617565#M74247</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2025-05-22T15:15:46Z</dc:date>
    </item>
    <item>
      <title>Re: Get Count returning wrong specified selected feature class</title>
      <link>https://community.esri.com/t5/python-questions/get-count-returning-wrong-specified-selected/m-p/1617571#M74248</link>
      <description>&lt;LI-CODE lang="python"&gt;def mapping(s, t, u, r):
    lot_site = dict()
    sale_site = dict()

    parcel_site =[]

    spatial_ref = arcpy.Describe(u).spatialReference

    for row in arcpy.da.SearchCursor(u, ['CountyPIN', 'SHAPE@X', 'SHAPE@Y']):
        if row[0] in t:
            lot_site.update({row[0] : (row[1], row[2])})

    for row in arcpy.da.SearchCursor(r,['PIN','SHAPE@X', 'SHAPE@Y']):
        if row[0] not in lot_site and row[0] in t:
            lot_site.update({row[0] : (row[1], row[2])})

    fc_feedlots = arcpy.CreateFeatureclass_management('in_memory', 'FeedPoint', 'POINT', '', '', '', spatial_ref)
    arcpy.AddField_management(fc_feedlots, 'PIN', 'TEXT')

    with arcpy.da.InsertCursor(fc_feedlots, ['SHAPE@', 'PIN']) as cur:
        for k, v in lot_site.iteritems():
            feed_point = [v[0], v[1]]
            cur.insertRow([feed_point, k])

    feedPoint_lyr = arcpy.MakeFeatureLayer_management(fc_feedlots, 'feedlot_lyr')
    address_lyr = arcpy.MakeFeatureLayer_management(u, 'Address_lyr')
    x = 0
    for row in arcpy.da.SearchCursor(feedPoint_lyr, ['SHAPE@', 'PIN']):
        select_feedlot = arcpy.SelectLayerByAttribute_management(feedPoint_lyr, "NEW_SELECTION", "PIN = '{}'".format(row[0]))
        sales = arcpy.management.SelectLayerByLocation(
                address_lyr, 'WITHIN_A_DISTANCE',
                row[0], '2640 FEET'
                )
        for sale in arcpy.da.SearchCursor(sales, ['CountyPIN']):
            if sale[0] in s:
                x += 1
                #sales_check(sale[0], sale_site)
                saleParcel = sale[0]
                feedParcel = row[0]
                feedPerm = t.get(row[0])[0]
                adj = s.get(sale[0])[0]
                netsale = s.get(sale[0])[1]
                pt = s.get(sale[0])[2]
                au_state = t.get(row[0])[1]
                ratio = s.get(sale[0])[3]
                count = int(arcpy.management.GetCount(feedPoint_lyr)[0])
            
                sale_site.update({x : [saleParcel, feedParcel, feedPerm, adj, netsale, pt, au_state, ratio, count]})
            
            
          

    arcpy.Delete_management('in_memory')
    print("Sale_site dictionary has been updated using Feedlots and Sales info")
    return sale_site&lt;/LI-CODE&gt;&lt;P&gt;My apologies! I didn't know that this was an option, I hope this is helpful!!&lt;/P&gt;</description>
      <pubDate>Thu, 22 May 2025 15:25:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/get-count-returning-wrong-specified-selected/m-p/1617571#M74248</guid>
      <dc:creator>GISIntern21</dc:creator>
      <dc:date>2025-05-22T15:25:32Z</dc:date>
    </item>
    <item>
      <title>Re: Get Count returning wrong specified selected feature class</title>
      <link>https://community.esri.com/t5/python-questions/get-count-returning-wrong-specified-selected/m-p/1617575#M74249</link>
      <description>&lt;P&gt;Also, where is the error happening in this?&lt;/P&gt;</description>
      <pubDate>Thu, 22 May 2025 15:34:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/get-count-returning-wrong-specified-selected/m-p/1617575#M74249</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2025-05-22T15:34:14Z</dc:date>
    </item>
    <item>
      <title>Re: Get Count returning wrong specified selected feature class</title>
      <link>https://community.esri.com/t5/python-questions/get-count-returning-wrong-specified-selected/m-p/1617609#M74250</link>
      <description>&lt;P&gt;Everything has been working up until the Select Layer by Location within that half mile to get the information regarding what points are within that ROI. My original script that I have that works just fine but provides the incorrect feedlot count is as follows:&lt;/P&gt;&lt;LI-CODE lang="c"&gt;#Identifies all sales within a 1/2 mile radius of feedlots, and creates a diction that will be passed into a new CSV output function.
# S- Sales Listing, T- Feedlot Location, U- Address Points, R- Tax Parcel polygons, V- Output CSV file
# K is refrencing the Keys within the Dictionaries.

def mapping(s, t, u, r):
    lot_site = dict()
    sale_site = dict()

    parcel_site =[]

    spatial_ref = arcpy.Describe(u).spatialReference

    for row in arcpy.da.SearchCursor(u, ['CountyPIN', 'SHAPE@X', 'SHAPE@Y']):
        if row[0] in t:
            lot_site.update({row[0] : (row[1], row[2])})

    for row in arcpy.da.SearchCursor(r,['PIN','SHAPE@X', 'SHAPE@Y']):
        if row[0] not in lot_site and row[0] in t:
            lot_site.update({row[0] : (row[1], row[2])})

    fc_feedlots = arcpy.CreateFeatureclass_management('in_memory', 'FeedPoint', 'POINT', '', '', '', spatial_ref)
    arcpy.AddField_management(fc_feedlots, 'PIN', 'TEXT')

    with arcpy.da.InsertCursor(fc_feedlots, ['SHAPE@', 'PIN']) as cur:
        for k, v in lot_site.iteritems():
            feed_point = [v[0], v[1]]
            cur.insertRow([feed_point, k])


    feedPoint_lyr = arcpy.MakeFeatureLayer_management(fc_feedlots, 'feedlot_lyr')
    address_lyr = arcpy.MakeFeatureLayer_management(u, 'Address_lyr')
    x = 0
    for row in arcpy.da.SearchCursor(feedPoint_lyr, ['PIN']):
        select_feedlot = arcpy.SelectLayerByAttribute_management(feedPoint_lyr, "NEW_SELECTION", "PIN = '{}'".format(row[0]))
        sales = arcpy.SelectLayerByLocation_management(address_lyr, 'WITHIN_A_DISTANCE', feedPoint_lyr, '2640 FEET', 'NEW_SELECTION')
        for sale in arcpy.da.SearchCursor(sales, ['CountyPIN']):
            if sale[0] in s:
                x += 1
                y = 1
                #sales_check(sale[0], sale_site)
                saleParcel = sale[0]
                feedParcel = row[0]
                feedPerm = t.get(row[0])[1]
                adj = s.get(sale[0])[0]
                netsale = s.get(sale[0])[1]
                au_state = t.get(row[0])[0]
                ratio = s.get(sale[0])[2]
                count = y

                sale_site.update({x : [saleParcel, feedParcel, feedPerm, adj, netsale, au_state, ratio, count]})

    arcpy.Delete_management('in_memory')

    return sale_site&lt;/LI-CODE&gt;&lt;P&gt;This script works, but again just provides me the incorrect feedlot count within a half mile of the sales points. When I run the script, it's only giving me '1' as a value for the feedlot count, but as we discussed, it's because the loop is only considering that one feedlot record or something of the sorts. I just need to find a way to manage a GetCount function into this script so that I can reach my final results.&lt;/P&gt;&lt;P&gt;Thank you for all the help you've been providing so far!!!&lt;/P&gt;</description>
      <pubDate>Thu, 22 May 2025 16:23:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/get-count-returning-wrong-specified-selected/m-p/1617609#M74250</guid>
      <dc:creator>GISIntern21</dc:creator>
      <dc:date>2025-05-22T16:23:44Z</dc:date>
    </item>
    <item>
      <title>Re: Get Count returning wrong specified selected feature class</title>
      <link>https://community.esri.com/t5/python-questions/get-count-returning-wrong-specified-selected/m-p/1617693#M74251</link>
      <description>&lt;P&gt;Hard to say without the actual data, and also hard to conceptualize your dictionary, but here's my guess of where to look:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;line 39: you set your variable&amp;nbsp;&lt;STRONG&gt;y&lt;/STRONG&gt; to 1&lt;UL&gt;&lt;LI&gt;on line 48, you set&amp;nbsp;&lt;STRONG&gt;count&lt;/STRONG&gt; to be y&lt;/LI&gt;&lt;LI&gt;So if you're looking specifically at "count" in the final dictionary, it's always going to be 1.&lt;UL&gt;&lt;LI&gt;One way to fix this is to copy&amp;nbsp;&lt;STRONG&gt;y=1&lt;/STRONG&gt;&amp;nbsp;(actually &lt;STRONG&gt;y=0&lt;/STRONG&gt;)&amp;nbsp;to outside that nested cursor, e.g. line 35-ish, and change what is currently line 39 to be &lt;STRONG&gt;y+=1&lt;/STRONG&gt;.&lt;/LI&gt;&lt;LI&gt;This would let you update the count as it loops through the sales, and reset it for each feed point.&lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;LI-CODE lang="python"&gt; for row in arcpy.da.SearchCursor(feedPoint_lyr, ['PIN']):
        select_feedlot = arcpy.SelectLayerByAttribute_management(
                               feedPoint_lyr, "NEW_SELECTION", 
                               "PIN = '{}'".format(row[0]))
        sales = arcpy.SelectLayerByLocation_management(
                      address_lyr, 'WITHIN_A_DISTANCE', 
                      feedPoint_lyr, '2640 FEET', 'NEW_SELECTION')
        y = 0
        for sale in arcpy.da.SearchCursor(sales, ['CountyPIN']):
            if sale[0] in s:
                x += 1
                y += 1
                #sales_check(sale[0], sale_site)
                saleParcel = sale[0]&lt;/LI-CODE&gt;&lt;P&gt;The other thing I'm really not sure of is what the purpose of&amp;nbsp;&lt;STRONG&gt;x&amp;nbsp;&lt;/STRONG&gt;is. You iterate it per sale and use it as a key in your&amp;nbsp;&lt;STRONG&gt;sale_site&lt;/STRONG&gt; dictionary, but what's the point? Is it doing the same thing as&amp;nbsp;&lt;STRONG&gt;y&lt;/STRONG&gt;?&lt;/P&gt;</description>
      <pubDate>Thu, 22 May 2025 18:01:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/get-count-returning-wrong-specified-selected/m-p/1617693#M74251</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2025-05-22T18:01:35Z</dc:date>
    </item>
    <item>
      <title>Re: Get Count returning wrong specified selected feature class</title>
      <link>https://community.esri.com/t5/python-questions/get-count-returning-wrong-specified-selected/m-p/1617796#M74255</link>
      <description>&lt;P&gt;I haven't had enough spare moments to dig through the full logic of your code or of some of the other options shared here, yet (hopefully this evening).&amp;nbsp; But at a glance, I'm still seeing both syntax &amp;amp; best practice issues in your cursor calls:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Line 28:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;From the screenshot:&amp;nbsp;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="MErikReedAugusta_0-1747948273334.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/133087i3A174C8D5ED94ACD/image-size/medium?v=v2&amp;amp;px=400" role="button" title="MErikReedAugusta_0-1747948273334.png" alt="MErikReedAugusta_0-1747948273334.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;From the code block:&amp;nbsp;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="MErikReedAugusta_1-1747948293897.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/133088iE4B68E1F1578136A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="MErikReedAugusta_1-1747948293897.png" alt="MErikReedAugusta_1-1747948293897.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Your two calls here don't match in the two versions you posted.&amp;nbsp; The first one isn't valid python, and the second one isn't the recommended format for cursor calls.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;with arcpy.da.SearchCursor(feedPoint_lyr, ['SHAPE@', 'PIN']) as cursor:
    for row in cursor:
        select_feedlot = [...] # Do stuff here
            # (Theoretically this would be Lines 29–48
            #   in your code, though that section has many
            #   syntax &amp;amp; logic problems, too, that I'll
            #   dig into later.)
del cursor&lt;/LI-CODE&gt;&lt;P&gt;This is the recommended syntax for your call in Line 28.&amp;nbsp; You really should be using context managers (the &lt;STRONG&gt;with&lt;/STRONG&gt; statement) in your cursor calls, and the "with row in [...] as cursor" isn't valid Python.&lt;/P&gt;&lt;P&gt;That example will be true of&amp;nbsp;&lt;STRONG&gt;all&lt;/STRONG&gt; of your Cursor calls (except&amp;nbsp;&lt;EM&gt;maybe&lt;/EM&gt; the InsertCursor; I'll need to review the broader logic to comment on that one).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Nested Cursors&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;You also still have a nested SearchCursor at Line 34 that's looping inside of the SearchCursor at Line 28.&amp;nbsp; As mentioned before, that's always at least a Yellow Flag, and sometimes an outright Red Flag in code.&amp;nbsp; Again a case where I'll need to follow through the broader logic when I find some time later.&lt;/P&gt;</description>
      <pubDate>Thu, 22 May 2025 21:22:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/get-count-returning-wrong-specified-selected/m-p/1617796#M74255</guid>
      <dc:creator>MErikReedAugusta</dc:creator>
      <dc:date>2025-05-22T21:22:00Z</dc:date>
    </item>
    <item>
      <title>Re: Get Count returning wrong specified selected feature class</title>
      <link>https://community.esri.com/t5/python-questions/get-count-returning-wrong-specified-selected/m-p/1617814#M74256</link>
      <description>&lt;P&gt;I've been ignoring the nested cursor in this case because it refers to just the layer created from selection a line above.&lt;/P&gt;</description>
      <pubDate>Thu, 22 May 2025 22:18:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/get-count-returning-wrong-specified-selected/m-p/1617814#M74256</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2025-05-22T22:18:54Z</dc:date>
    </item>
    <item>
      <title>Re: Get Count returning wrong specified selected feature class</title>
      <link>https://community.esri.com/t5/python-questions/get-count-returning-wrong-specified-selected/m-p/1617820#M74257</link>
      <description>&lt;P&gt;Okay, so I took some time and went through this whole script, and I think your first problem is that your code is unnecessarily convoluted.&amp;nbsp; It's not that it's bad,&amp;nbsp;&lt;EM&gt;per se&lt;/EM&gt;, but that it's not doing you any favors troubleshooting and identifying the code.&amp;nbsp; There are also some logical analysis concerns, here.&amp;nbsp; So I'm going to go down the rabbit hole and try to address all of it that I caught.&lt;/P&gt;&lt;P&gt;This is going to be long, so I'm also going to color code the headings (and possibly break the post in two, if I hit forum limits).&amp;nbsp; I'll also put a letter tag for the color, in case you or anyone else is color-blind:&amp;nbsp;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;FONT color="#339966"&gt;&lt;STRONG&gt;[G] Green Headings&lt;/STRONG&gt;&lt;/FONT&gt; mean you really&amp;nbsp;&lt;EM&gt;should&lt;/EM&gt; do this, but technically it'll probably work if you don't.&amp;nbsp; I'll give my justifications why I think the change is worth it, though.&amp;nbsp;&lt;/LI&gt;&lt;LI&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;[R] Red Headings&lt;/STRONG&gt;&lt;/FONT&gt; mean you&amp;nbsp;&lt;STRONG&gt;need&lt;/STRONG&gt; to do this.&amp;nbsp; Either it's a syntax error or a serious logical error, or maybe it's just a massively bad practice that goes beyond "should".&lt;/LI&gt;&lt;LI&gt;&lt;FONT color="#800080"&gt;&lt;STRONG&gt;[P] Purple Headings&lt;/STRONG&gt;&lt;/FONT&gt; are sidenotes.&amp;nbsp; They might not require any action, but they're generally assumptions or thoughts I had while parsing through; if any are wrong, it might actually be a red flag.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;Also, I'm going to refer to variable names in &lt;STRONG&gt;&lt;FONT color="#3366FF"&gt;bolded blue&lt;/FONT&gt;&lt;/STRONG&gt;, since these boards lack a good inline code option for things that don't warrant a full block.&lt;/P&gt;&lt;P&gt;&lt;FONT color="#339966"&gt;&lt;STRONG&gt;1a. [G] Variable Names (&lt;EM&gt;especially&lt;/EM&gt; your input variables)&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;Single-letter variable names like the (s, t, u, r) in your function definition&amp;nbsp;&lt;EM&gt;really&lt;/EM&gt; aren't the time saver many coders think they are.&amp;nbsp; Trust me when I tell you, the 1–2 seconds longer it takes you to type out &lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;sales_listing&lt;/STRONG&gt;&lt;/FONT&gt;&amp;nbsp;instead of &lt;STRONG&gt;&lt;FONT color="#3366FF"&gt;s&lt;/FONT&gt;&lt;/STRONG&gt;&amp;nbsp;is irrelevant against the mental load it's going to take you to troubleshoot and especially the mental load it's going to take&amp;nbsp;&lt;EM&gt;others&lt;/EM&gt; to troubleshoot.&amp;nbsp; And too often forgotten: "Others" also includes&amp;nbsp;&lt;STRONG&gt;yourself&lt;/STRONG&gt;, but months in the future, when someone asks you to run this code again and it breaks, or you need to modify something to alter the calculation you're running.&amp;nbsp; Be nice to future you; write longer variable names, and do at least&amp;nbsp;&lt;EM&gt;some&lt;/EM&gt; rudimentary documentation as you go.&amp;nbsp; "Schedule time for machine maintenance, or your machine will schedule it &lt;U&gt;&lt;EM&gt;for&lt;/EM&gt;&lt;/U&gt; you," as the saying goes.&lt;/P&gt;&lt;P&gt;&lt;FONT color="#800080"&gt;&lt;STRONG&gt;1b. [P] Try out an IDE other than IDLE&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;This is 99% a personal preference thing, so you do you.&amp;nbsp; I personally use PyCharm and quite like it, but everyone's different.&amp;nbsp; Two benefits relevant to addressing this script in particular, though: Auto-completion will help those longer variable names like &lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;sales_listing&lt;/STRONG&gt;&lt;/FONT&gt;&amp;nbsp;from 1a take the same amount of time to type as &lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;s&lt;/STRONG&gt;&lt;/FONT&gt;.&amp;nbsp; Now you get all the benefits of longer names, without any of the downsides.&amp;nbsp; Also, most other IDEs will do things like highlight unused variables.&amp;nbsp; You might've missed this entirely, but &lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;parcel_sites&lt;/STRONG&gt;&lt;/FONT&gt;&amp;nbsp;and &lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;spatial_ref&lt;/STRONG&gt;&lt;/FONT&gt;&amp;nbsp;don't actually&amp;nbsp;&lt;EM&gt;&lt;STRONG&gt;do&lt;/STRONG&gt;&lt;/EM&gt; anything, other than get created &amp;amp; forgotten in Lines 9 &amp;amp; 11.&amp;nbsp; They're not realistically hurting anything, but the extra clutter is going to make it harder to troubleshoot problems in your code.&amp;nbsp; The same is true of &lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;select_feedlot&lt;/STRONG&gt;&lt;/FONT&gt; in Line 34, though that might be another error we'll get to.&lt;/P&gt;&lt;P&gt;&lt;FONT color="#339966"&gt;&lt;STRONG&gt;2. [G/&lt;FONT color="#FF0000"&gt;R]&lt;/FONT&gt; Source Parcel Data &lt;FONT color="#FF0000"&gt;&amp;amp; Overwrites&lt;BR /&gt;&lt;FONT color="#000000"&gt;(Possibly harmless; possibly catastrophic—depending on source data)&lt;/FONT&gt;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P data-unlink="true"&gt;First note: The way you're writing these means that PINs present in the Tax Parcel polygons will overwrite any matching PINs in the Address Points.&amp;nbsp; If your source data is anything like our municipality's, the locations of the Address Points and the locations of the&amp;nbsp;&lt;EM&gt;centroids&lt;/EM&gt; of the Parcels&amp;nbsp;(as represented by &lt;STRONG&gt;&lt;FONT color="#3366FF"&gt;SHAPE@X&lt;/FONT&gt;&lt;/STRONG&gt;&amp;nbsp;&amp;amp; &lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;SHAPE@Y&lt;/STRONG&gt;&lt;/FONT&gt;&amp;nbsp;in Line 17) likely don't match.&amp;nbsp; And if the parcels are big enough or close enough to that half-mile line, you could end up with lots of false positives and/or false negatives, depending on which point is more "correct".&lt;/P&gt;&lt;P data-unlink="true"&gt;If you can be sure that all PINs in one data set exist in the other dataset, then I'd just import one, and ignore the other.&amp;nbsp; If there are unique PINs in either, then you'll need to decide which one wins, and make sure that one is second.&amp;nbsp; But know that's going to increase the error bars in your resulting analysis.&lt;/P&gt;&lt;P data-unlink="true"&gt;Also, if you&amp;nbsp;&lt;EM&gt;can&lt;/EM&gt; use a single dataset, then Lines 21–27 become irrelevant, saving you and your computer some work!&lt;/P&gt;&lt;P data-unlink="true"&gt;&lt;FONT color="#993366"&gt;&lt;STRONG&gt;2b. [P] dict.iteritems() vs dict.items()&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P data-unlink="true"&gt;In Line 25, you call iteritems(), but that method was removed for Python 3.&amp;nbsp; If you're in ArcGIS Pro, you need to be writing in Python 3, even if your code is running outside Pro.&amp;nbsp; If you're in ArcGIS Desktop, then there might be bigger concerns elsewhere, but for now this will presume you're working in Pro/Python 3.&amp;nbsp; If you can't go down to one dataset (per #2), then you want lot_site.items().&lt;/P&gt;&lt;P data-unlink="true"&gt;For more information of the difference, there's a pretty good explanation in &lt;A href="https://stackoverflow.com/questions/10458437/what-is-the-difference-between-dict-items-and-dict-iteritems-in-python-2" target="_self"&gt;the Stack Exchange question here&lt;/A&gt;.&lt;/P&gt;&lt;P data-unlink="true"&gt;&lt;FONT color="#339966"&gt;&lt;STRONG&gt;2c. [G] Unpacking &amp;amp; repacking lists/tuples&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;v = (x, y)
# These two lines do the same thing, but notice one is MUCH clearer.  Why
# unpack &amp;amp; repack?  The only difference is tuple vs. list.  You could just
# save your original as a list, or cast it with list(v), instead. Much easier
# to debug later.

feed_point = [v[0],v[1]]
feed_point = v&lt;/LI-CODE&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;&amp;nbsp;&lt;STRONG&gt;3. [R] SelectLayerByLocation against selected parcel or parcel coordinates?&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;In Line 34, you do a SelectLayerByAttribute to get just the parcel you're looking at in this iteration of the loop.&amp;nbsp; Then, you feed in the geometry token from the loop into SLBL.&amp;nbsp; The parcel SLBA doesn't do anything.&lt;/P&gt;&lt;P&gt;Worse, this is the outer Cursor of your nested cursors, and it doesn't appear necessary at all.&amp;nbsp; Lines 13–27 create a Feature Class that you then iterate through in Line 33, but you could just as easily have been iterating through a dictionary in far fewer lines:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;lot_site = {}
with arcpy.da.SearchCursor(address_points, ['CountyPIN', 'SHAPE@']) as cur1:
  for pin, geo in cur1:
    lot_site[pin] = geo
with arcpy.da.SearchCursor(tax_parcels, ['PIN', 'SHAPE@']) as cur1:
  for pin, geo in cur1:
    lot_site[pin] = geo

for pin, geo in lot_site.items():
  saleAddresses = arcpy.management.SelectLayerByLocation(address_lyr,
                                                         'WITHIN_A_DISTANCE',
                                                         geo,
                                                         '2640 FEET')
  # do other stuff&lt;/LI-CODE&gt;&lt;P&gt;As stated above in #2, you might not even need Lines 2–4 of the code above!&lt;/P&gt;&lt;P&gt;&lt;FONT color="#339966"&gt;&lt;STRONG&gt;4. [G] Inner Cursor vs Definition Query&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;The other reason you have nested cursors is because you only want to pull the PINs that are in your Sales Listing.&amp;nbsp; There's a nonzero chance this could be done by applying a definition query to the PIN data source(s) you're reading in.&amp;nbsp; Examples are beyond the scope of this already-mammoth post, because they depend on how you're feeding the source PIN data in.&amp;nbsp; But it's yet another case of "If you need to nest cursors, take a second look at your basic structural assumptions."&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;5. [R] Variable names again, but&amp;nbsp;&lt;EM&gt;way&lt;/EM&gt; more of a mess.&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;It took me absurdly long to parse through Lines 41–47.&amp;nbsp; Partly, that was because of the nested cursors, and partly that was because of all the tuple slicing going on.&amp;nbsp; Instead of calling &lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;sale[0]&lt;/STRONG&gt;&lt;/FONT&gt; every time, just save it to a variable.&amp;nbsp; In my test case where I was scribbling notes, I called it &lt;STRONG&gt;&lt;FONT color="#3366FF"&gt;innerPIN&lt;/FONT&gt;&lt;/STRONG&gt;, but I still maintain that the nested cursors aren't necessary here.&amp;nbsp; You&amp;nbsp;&lt;EM&gt;might&lt;/EM&gt; also be able to combine some of those lines, depending on the structure of the data it's reading from the Sales &amp;amp; Feedlot listings:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;adj, netsale, ratio = s.get(innerPIN)&lt;/LI-CODE&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;6. [R] I think you missed a step...&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;Lastly, the most critical problem appears to be in Lines 38–48.&amp;nbsp; As pointed out by Alfred: In Line 39, you set &lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;y=1&lt;/STRONG&gt;&lt;/FONT&gt;.&amp;nbsp; Every PIN; every time.&amp;nbsp; Then, you pass that 1 over to &lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;count&lt;/STRONG&gt;&lt;/FONT&gt;, which you save in your output dictionary.&amp;nbsp; Every time.&lt;/P&gt;&lt;P&gt;No matter what the rest of this script does, you're&amp;nbsp;&lt;EM&gt;always&lt;/EM&gt; going to get a count of 1, because you manually passed that in.&amp;nbsp; But I suspect you missed it because of the complications elsewhere, which is why I wrote out all that stuff above.&amp;nbsp; At a loose glance, you appear to be missing a step, but to be sure, I would need to see an example output dictionary of what you expect to get back.&amp;nbsp; You can put dummy PINs and such in, if you're worried about confidentiality concerns.&amp;nbsp; Instead of the usual string of numbers and dashes most municipalities use, just use "dummy-PIN-1" and "dummy-PIN-2" in your example.&lt;/P&gt;</description>
      <pubDate>Thu, 22 May 2025 22:46:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/get-count-returning-wrong-specified-selected/m-p/1617820#M74257</guid>
      <dc:creator>MErikReedAugusta</dc:creator>
      <dc:date>2025-05-22T22:46:41Z</dc:date>
    </item>
    <item>
      <title>Re: Get Count returning wrong specified selected feature class</title>
      <link>https://community.esri.com/t5/python-questions/get-count-returning-wrong-specified-selected/m-p/1619649#M74307</link>
      <description>&lt;P&gt;Hello! My apologies for the delay in a response, I've been making a lot of changes to the script!&amp;nbsp;&lt;BR /&gt;After looking over everything, I made a lot of tweaks and edits.. I did end up getting rid of the Parcel Polygon layer, as we don't actually need it. I did also skim down some of the Search Cursors as well to make everything function and overall look much more simpler and less messy. Everything is running as it should, but I am still not getting the Get Count down for whatever reason.. I tried implementing the 'Y += 1' suggestion, but when I get my output in my CSV, it is returning an overall count of the amount of times a feedlot is recorded "ex: 1, 2, 3, etc" instead of just an overall total. My end goal is to get the amount of feedlots within a half mile of each address point I am getting, so if 1 address point has 3 feedlots within that half mile, I want it to reflect that result in my count and CSV output. Any suggestions for what I should do next, or any changes I could/should make to my updated script? The overall run time I am getting is around 1-1.5 minutes.&lt;/P&gt;&lt;LI-CODE lang="c"&gt;def sales_listing(file_location):

    workbook = xlrd.open_workbook(file_location)
    sheet = workbook.sheet_by_name("SalesListing")
  
    sales_dict = dict()

    for row in range(1, sheet.nrows):
        parcelID = str(sheet.cell(row, 2).value).replace('.','')
        sales_dict.update({parcelID :  [int(sheet.cell(row, 8).value), str(sheet.cell(row, 20).value), str(sheet.cell(row, 9).value), float(sheet.cell(row, 21).value)]})

    return sales_dict
    

########### Geo work


def mapping(s, t, u):

    addy_site = dict()
    sale_site = dict()

    parcel_site =[]
    spatial_ref = arcpy.Describe(u).spatialReference


    fc_salesListing = arcpy.CreateFeatureclass_management('in_memory', 'SalesPoint', 'POINT', '', '', '', spatial_ref)
    arcpy.AddField_management(fc_salesListing, 'PIN', 'TEXT')

    for row in arcpy.da.SearchCursor(u, ['CountyPIN', 'SHAPE@X', 'SHAPE@Y']):
        if row[0] in s:
            addy_site.update({row[0] : (row[1], row[2])})
            
    with arcpy.da.InsertCursor(fc_salesListing, ['SHAPE@', 'PIN']) as cur:
        for k, v in addy_site.items():
            addy_point = v
            cur.insertRow([addy_point, k])


    feedPoint_lyr = arcpy.MakeFeatureLayer_management(t, 'feedlot_lyr')
    address_lyr = arcpy.MakeFeatureLayer_management(fc_salesListing, 'Address_lyr')
    x = 0

    for row in arcpy.da.SearchCursor(feedPoint_lyr, ['ParcelID', 'AU_State', 'Permit_Num']):
        select_feedlot = arcpy.SelectLayerByAttribute_management(feedPoint_lyr, "NEW_SELECTION", "parcelID = '{}'".format(row[0]))
        sales = arcpy.SelectLayerByLocation_management(address_lyr, 'WITHIN_A_DISTANCE', feedPoint_lyr, '2640 FEET', 'NEW_SELECTION')
        y = 0
        for sale in arcpy.da.SearchCursor(sales, ['PIN']):
            if sale[0] in s:
                x += 1
                y += 1
                #sales_check(sale[0], sale_site)
                saleParcel = sale[0]
                feedParcel = row[0]
                feedPerm = row[2]
                adj = s.get(sale[0])[0]
                netsale = s.get(sale[0])[1]
                pt = s.get(sale[0])[2]
                au_state = row[1]
                ratio = s.get(sale[0])[3]
                count = y

                sale_site.update({x : [saleParcel, feedParcel, feedPerm, adj, netsale, pt, au_state, ratio, count]})


    arcpy.Delete_management('in_memory')
    return sale_site&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 30 May 2025 16:39:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/get-count-returning-wrong-specified-selected/m-p/1619649#M74307</guid>
      <dc:creator>GISIntern21</dc:creator>
      <dc:date>2025-05-30T16:39:30Z</dc:date>
    </item>
  </channel>
</rss>

