<?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: New to Python in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/new-to-python/m-p/1267850#M67104</link>
    <description>&lt;P&gt;It's already a set, and sort() produces a list from the set.&lt;/P&gt;</description>
    <pubDate>Wed, 15 Mar 2023 00:57:32 GMT</pubDate>
    <dc:creator>DavidPike</dc:creator>
    <dc:date>2023-03-15T00:57:32Z</dc:date>
    <item>
      <title>New to Python</title>
      <link>https://community.esri.com/t5/python-questions/new-to-python/m-p/1267748#M67090</link>
      <description>&lt;P&gt;I am having trouble with a file path or the format of the destination I believe. I am very new to Python and have pieced this together with a lot of questions without a strong understanding of ArcGIS yet.&lt;/P&gt;&lt;LI-CODE lang="c"&gt;# Imports
import arcpy

# Workspace
trail_data = r"C:\PythonPro\Final\Trail"
county_data = r"C:\PythonPro\Final\Counties\Oregon_Counties.shp"

# Get a list of unique county names
counties = []
with arcpy.da.SearchCursor(county_data, "County_Name") as cursor:
    for row in cursor:
        if row[0] not in counties:
            counties.append(row[0])

# Print the list of counties and ask the user to choose one
print("Choose a county: ")
for i, county in enumerate(counties):
    print(f"{i+1}. {county}")
selection = int(input("&amp;gt; ")) - 1
selected_county = counties[selection]
print("Measuring...")

# Use the selected county to clip the trail data
clip_output = "C:/data/clipped_trails.shp"
where_clause = f"County_Name = '{selected_county}'"
arcpy.Clip_analysis(trail_data, county_data, clip_output, where_clause)

# Calculate the total distance of bike trails in the clipped data
total_distance = 0
with arcpy.da.SearchCursor(clip_output, "SHAPE@LENGTH") as cursor:
    for row in cursor:
        total_distance += row[0]

# Display the total distance to the user
print(f"The total distance of bike trails in {selected_county} is {total_distance:.2f} meters.")
&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The error code I am getting is: RuntimeError: Cannot find field 'County_Name'&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have tried to change the source and the configuration of the files on my PC. I suspect that the problem is something simple that I am missing, but I am banging my head against a wall and can't see it clearly. Any help is appreciated. Thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Chris&lt;/P&gt;</description>
      <pubDate>Tue, 14 Mar 2023 21:21:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/new-to-python/m-p/1267748#M67090</guid>
      <dc:creator>ChrisGlenn</dc:creator>
      <dc:date>2023-03-14T21:21:01Z</dc:date>
    </item>
    <item>
      <title>Re: New to Python</title>
      <link>https://community.esri.com/t5/python-questions/new-to-python/m-p/1267756#M67091</link>
      <description>&lt;P&gt;Please format your code for readability, it's also lost its indentation.&amp;nbsp;&amp;nbsp;&lt;A href="https://community.esri.com/t5/python-blog/code-formatting-the-community-version/ba-p/1007633" target="_blank"&gt;https://community.esri.com/t5/python-blog/code-formatting-the-community-version/ba-p/1007633&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Do you know what line it's erroring on? that would save a lot of debugging time.&amp;nbsp; Perhaps comment out each section and run it sequentially uncommenting lines to see where the error is.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;##what is trail data? Looks like a workspace/folder but you're using it as a clip input?
trail_data = r"C:\PythonPro\Trail"

arcpy.Clip_analysis(trail_data, county_data, clip_output, where_clause)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Mar 2023 20:00:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/new-to-python/m-p/1267756#M67091</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2023-03-14T20:00:11Z</dc:date>
    </item>
    <item>
      <title>Re: New to Python</title>
      <link>https://community.esri.com/t5/python-questions/new-to-python/m-p/1267757#M67092</link>
      <description>&lt;P&gt;&lt;A href="https://community.esri.com/t5/python-blog/code-formatting-the-community-version/ba-p/1007633" target="_blank"&gt;Code formatting ... the Community Version - Esri Community&lt;/A&gt;&lt;/P&gt;&lt;P&gt;"county_data"&amp;nbsp; is a folder, so if it is a geodatabase, then you need to provide the gdb name and the featureclassname.&amp;nbsp; If it is a shapefile you need the *.shp&lt;/P&gt;&lt;P&gt;eg&amp;nbsp;&amp;nbsp;county_data = r"C:\PythonPro\Counties.gdb\MyFeatureclass"&lt;/P&gt;</description>
      <pubDate>Tue, 14 Mar 2023 20:01:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/new-to-python/m-p/1267757#M67092</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2023-03-14T20:01:52Z</dc:date>
    </item>
    <item>
      <title>Re: New to Python</title>
      <link>https://community.esri.com/t5/python-questions/new-to-python/m-p/1267767#M67093</link>
      <description>&lt;P&gt;I think since it is not obvious that what type of file you are reading (shape file, geodatabase, etc) it is not easy to go further in troubleshooting as that might be the initial issue.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then the second issue might be the field (column header) name that you are passing does not exist.&amp;nbsp; You can check for that like this:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;fields = arcpy.ListFields(country_data)  # list of objects
field_names = [i.name for i in fields]  # list of field names
#check if 'Country_Name' is in the list of names
# instead of one field name you can also pass a list of them.
if 'County_Name' in field_names:
    counties = []
    with arcpy.da.SearchCursor(county_data, "County_Name") as cursor:
        for row in cursor:
            if row[0] not in counties:
                counties.append(row[0])
else:
     print('County_Name not in the fields')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Mar 2023 21:42:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/new-to-python/m-p/1267767#M67093</guid>
      <dc:creator>Mahdi_Ch</dc:creator>
      <dc:date>2023-03-14T21:42:17Z</dc:date>
    </item>
    <item>
      <title>Re: New to Python</title>
      <link>https://community.esri.com/t5/python-questions/new-to-python/m-p/1267775#M67096</link>
      <description>&lt;P&gt;By the way , assuming the 'County_Name' exists, if you are looping to get the unique values of that field, there is a faster (at least faster to write) cleaner Pythonic way, by using set.&lt;/P&gt;&lt;P&gt;A set is an unordered collection with no duplicate elements. So basically,&amp;nbsp;&amp;nbsp;instead of going line by line and assigning the values after checking if they already exist, you can use a "set comprehension" to add all the values to the set function. And then Python set() do the work behind the scene and returns only the unique values for you.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;with arcpy.da.SearchCursor(county_data, "County_Name") as cursor:
    # extracting unique values in the field using Python set function and set comprehension 
    counties= sorted({row[0] for row in cursor}) &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then the sorted function -obviously- sorts the values and returns them in a &lt;U&gt;list.&lt;/U&gt;&lt;/P&gt;&lt;P&gt;Please follow what&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/167692"&gt;@DavidPike&lt;/a&gt;&amp;nbsp; sent you to make your code readable.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Mar 2023 04:33:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/new-to-python/m-p/1267775#M67096</guid>
      <dc:creator>Mahdi_Ch</dc:creator>
      <dc:date>2023-03-15T04:33:59Z</dc:date>
    </item>
    <item>
      <title>Re: New to Python</title>
      <link>https://community.esri.com/t5/python-questions/new-to-python/m-p/1267785#M67097</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/662373"&gt;@Mahdi_Ch&lt;/a&gt;&amp;nbsp; I think it would be useful if you explained in the code comments the logic of writing to a set,&amp;nbsp; would be a good thing to understand for beginners.&lt;/P&gt;</description>
      <pubDate>Tue, 14 Mar 2023 20:53:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/new-to-python/m-p/1267785#M67097</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2023-03-14T20:53:58Z</dc:date>
    </item>
    <item>
      <title>Re: New to Python</title>
      <link>https://community.esri.com/t5/python-questions/new-to-python/m-p/1267791#M67098</link>
      <description>&lt;LI-CODE lang="c"&gt;# Imports
import arcpy

# Workspace
trail_data = r"C:\PythonPro\Final\Trail"
county_data = r"C:\PythonPro\Final\Counties\Oregon_Counties.shp"

# Get a list of unique county names
counties = []
with arcpy.da.SearchCursor(county_data, "County_Name") as cursor:
    for row in cursor:
        if row[0] not in counties:
            counties.append(row[0])

# Print the list of counties and ask the user to choose one
print("Choose a county: ")
for i, county in enumerate(counties):
    print(f"{i+1}. {county}")
selection = int(input("&amp;gt; ")) - 1
selected_county = counties[selection]
print("Measuring...")

# Use the selected county to clip the trail data
clip_output = "C:/data/clipped_trails.shp"
where_clause = f"County_Name = '{selected_county}'"
arcpy.Clip_analysis(trail_data, county_data, clip_output, where_clause)

# Calculate the total distance of bike trails in the clipped data
total_distance = 0
with arcpy.da.SearchCursor(clip_output, "SHAPE@LENGTH") as cursor:
    for row in cursor:
        total_distance += row[0]

# Display the total distance to the user
print(f"The total distance of bike trails in {selected_county} is {total_distance:.2f} meters.")
&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 14 Mar 2023 21:10:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/new-to-python/m-p/1267791#M67098</guid>
      <dc:creator>ChrisGlenn</dc:creator>
      <dc:date>2023-03-14T21:10:27Z</dc:date>
    </item>
    <item>
      <title>Re: New to Python</title>
      <link>https://community.esri.com/t5/python-questions/new-to-python/m-p/1267793#M67099</link>
      <description>&lt;P&gt;Apologies for the incorrect format. Here is what I have. I've tried to download the layer I am using as a .shp and .gdb and have had similar issues.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Line 17 is where the error code comes from.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Mar 2023 21:16:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/new-to-python/m-p/1267793#M67099</guid>
      <dc:creator>ChrisGlenn</dc:creator>
      <dc:date>2023-03-14T21:16:10Z</dc:date>
    </item>
    <item>
      <title>Re: New to Python</title>
      <link>https://community.esri.com/t5/python-questions/new-to-python/m-p/1267809#M67101</link>
      <description>&lt;P&gt;Line 17 doesn't make much sense.&amp;nbsp; the error is probably coming from your search cursor.&amp;nbsp; Are you sure county_name exists in the data rather than being some result of a join on layer object?&amp;nbsp; Are you also sure county_name is the field name rather than an alias?&lt;/P&gt;</description>
      <pubDate>Tue, 14 Mar 2023 21:40:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/new-to-python/m-p/1267809#M67101</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2023-03-14T21:40:37Z</dc:date>
    </item>
    <item>
      <title>Re: New to Python</title>
      <link>https://community.esri.com/t5/python-questions/new-to-python/m-p/1267811#M67102</link>
      <description>&lt;P&gt;David, you are totally right. I edited my post and included the comments.&amp;nbsp; Thank you for mentioning that.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Mar 2023 21:57:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/new-to-python/m-p/1267811#M67102</guid>
      <dc:creator>Mahdi_Ch</dc:creator>
      <dc:date>2023-03-14T21:57:45Z</dc:date>
    </item>
    <item>
      <title>Re: New to Python</title>
      <link>https://community.esri.com/t5/python-questions/new-to-python/m-p/1267849#M67103</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;P&gt;&lt;BR /&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/662373"&gt;@Mahdi_Ch&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;A set is an unordered collection with no duplicate elements. So basically,&amp;nbsp;&amp;nbsp;instead of going line by line and assigning the values after checking if they already exist, you can use a "set comprehension" to add all the values to the set function. And then Python set() do the work behind the scene and returns only the unique values for you.&amp;nbsp;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Nice explanation! Now one line it! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;counties = list(set([row[0] for row in arcpy.da.SearchCursor(fc, "County_Name")]))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Mar 2023 00:54:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/new-to-python/m-p/1267849#M67103</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2023-03-15T00:54:11Z</dc:date>
    </item>
    <item>
      <title>Re: New to Python</title>
      <link>https://community.esri.com/t5/python-questions/new-to-python/m-p/1267850#M67104</link>
      <description>&lt;P&gt;It's already a set, and sort() produces a list from the set.&lt;/P&gt;</description>
      <pubDate>Wed, 15 Mar 2023 00:57:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/new-to-python/m-p/1267850#M67104</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2023-03-15T00:57:32Z</dc:date>
    </item>
    <item>
      <title>Re: New to Python</title>
      <link>https://community.esri.com/t5/python-questions/new-to-python/m-p/1267863#M67105</link>
      <description>&lt;P&gt;I'm aware- sorry if that was misguided... I was just showing an all inclusive version of the cursor with list comprehension.&lt;/P&gt;</description>
      <pubDate>Wed, 15 Mar 2023 01:26:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/new-to-python/m-p/1267863#M67105</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2023-03-15T01:26:37Z</dc:date>
    </item>
    <item>
      <title>Re: New to Python</title>
      <link>https://community.esri.com/t5/python-questions/new-to-python/m-p/1267886#M67106</link>
      <description>&lt;P&gt;@Anonymous User&amp;nbsp;that was hilarious!&amp;nbsp; So, waiting for the next person to do that in zero line!&amp;nbsp;&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":face_with_tears_of_joy:"&gt;😂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Jokes aside, that's cool. And I love list comprehensions. I used to write pretty long ones but after a while I realized that there are certain problems associated with &lt;U&gt;long list comprehensions.&lt;/U&gt;&lt;/P&gt;&lt;P&gt;First, after a certain point it negatively affects the readability and it becomes harder to read and understand the logic.&lt;BR /&gt;Second, in case of errors, since a lot is happening inside the list -without making intermediate variables available to us- that makes it very hard to debug.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I thought it worth mentioning for those who just learned about this concept.&lt;/P&gt;&lt;P&gt;BTW, If you want to go rogue on that,&amp;nbsp; you can throw some ifs and lambdas there and then watch it burn.&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":beaming_face_with_smiling_eyes:"&gt;😁&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Mar 2023 05:07:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/new-to-python/m-p/1267886#M67106</guid>
      <dc:creator>Mahdi_Ch</dc:creator>
      <dc:date>2023-03-15T05:07:30Z</dc:date>
    </item>
  </channel>
</rss>

