<?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: Best way to add values to a string for UpdateCursor in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/best-way-to-add-values-to-a-string-for/m-p/1076369#M61609</link>
    <description>&lt;P&gt;You might get a Key Error in 6 if the label is not in the gridList.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;gridList_pageName = gridList.get(label, None)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;Will default the gridList_pageName to None if the key is not found and will continue execution.&lt;/P&gt;</description>
    <pubDate>Wed, 07 Jul 2021 16:55:21 GMT</pubDate>
    <dc:creator>Anonymous User</dc:creator>
    <dc:date>2021-07-07T16:55:21Z</dc:date>
    <item>
      <title>Best way to add values to a string for UpdateCursor</title>
      <link>https://community.esri.com/t5/python-questions/best-way-to-add-values-to-a-string-for/m-p/1076109#M61586</link>
      <description>&lt;P&gt;I am still really new to Python and ArcPy. We have a pair of feature classes, one with an index grid and the other with project locations. We are trying to create a table that shows what index grid cells each project intersects.&lt;/P&gt;&lt;P&gt;I have a script that performs&amp;nbsp;&lt;STRONG&gt;Intersect&lt;/STRONG&gt; on the two feature classes, creates a new table (&lt;EM&gt;indexTable&lt;/EM&gt;) , adds the two needed fields: &lt;EM&gt;Label&lt;/EM&gt; (the project number) and &lt;EM&gt;PageName&lt;/EM&gt; (the index grid), and populates &lt;EM&gt;Label&lt;/EM&gt; using an InsertCursor.&amp;nbsp;I know that I eventually need to use an UpdateCursor with an update string to update PageName, but I am at a loss on how to properly do it, since some of the projects intersect more than one grid cell.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Currently, I have a SearchCursor to get the data from the Intersect feature class, but it breaks with "&lt;EM&gt;&lt;STRONG&gt;SystemError: error return without exception se&lt;/STRONG&gt;&lt;STRONG&gt;t&lt;/STRONG&gt;&lt;/EM&gt;".&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# UpdateCursor to add all index grid cells for each label
gridList = [r for r in arcpy.da.SearchCursor(intersectOut, ("Label", "PageName"))]  # List from intersection
arcpy.AddMessage("Grid list = {}".format(gridList))  # For debugging
with arcpy.da.UpdateCursor(indexTable, ("Label", "PageName")) as uCursor:
    for row in uCursor:
        name_field = "Label"
        expression = u'{} = {}'.format(arcpy.AddFieldDelimiters(intersectOut, name_field), row[0])
        # expression = "Label = '" + row[0] + "'"
        with arcpy.da.SearchCursor(intersectOut, ("Label", "PageName"), where_clause=expression) as sCursor:
            gridString = ""
            if gridString == "":
                gridString = gridString + sCursor[1]
            else:
                gridString = gridString + ", " + sCursor[1]

        arcpy.AddMessage(gridString)
    # Before going to the next record in the site index table write this list to the appropriate field.
        row[1] = gridString
        uCursor.updateRow(row)&lt;/LI-CODE&gt;&lt;P&gt;I also tried using list comprehension to go through gridList, based on row[0] of the UpdateCursor, but that just returns a series of empty lists--and I was unsure where to go from there in any case.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# UpdateCursor to add all index grid cells for each label
gridList = [r for r in arcpy.da.SearchCursor(intersectOut, ("Label", "PageName"))]  # List from intersection
arcpy.AddMessage("Grid list = {}".format(gridList))  # For debugging
with arcpy.da.UpdateCursor(indexTable, ("Label", "PageName")) as uCursor:
    for row in uCursor:
        search = row[0]
        arcpy.AddMessage(search)
        filterList = [r for r in gridList if gridList[0] == search]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I know there has to be a way to do this, but I am at a loss.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Jul 2021 22:43:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-way-to-add-values-to-a-string-for/m-p/1076109#M61586</guid>
      <dc:creator>JamesWilcox1970</dc:creator>
      <dc:date>2021-07-06T22:43:03Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to add values to a string for UpdateCursor</title>
      <link>https://community.esri.com/t5/python-questions/best-way-to-add-values-to-a-string-for/m-p/1076130#M61587</link>
      <description>&lt;P&gt;what is line 10 and 11 doing?&amp;nbsp; you never get to 13 and 14&lt;/P&gt;&lt;P&gt;sample messages? or does it fail before it even gets to an addmessage?&lt;/P&gt;</description>
      <pubDate>Tue, 06 Jul 2021 23:20:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-way-to-add-values-to-a-string-for/m-p/1076130#M61587</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-07-06T23:20:06Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to add values to a string for UpdateCursor</title>
      <link>https://community.esri.com/t5/python-questions/best-way-to-add-values-to-a-string-for/m-p/1076132#M61588</link>
      <description>&lt;P&gt;Lines 10-14 are supposed to build the output string for the UpdateCursor...once it gets there. But it doesn't get there.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Jul 2021 23:23:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-way-to-add-values-to-a-string-for/m-p/1076132#M61588</guid>
      <dc:creator>JamesWilcox1970</dc:creator>
      <dc:date>2021-07-06T23:23:00Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to add values to a string for UpdateCursor</title>
      <link>https://community.esri.com/t5/python-questions/best-way-to-add-values-to-a-string-for/m-p/1076144#M61589</link>
      <description>&lt;P&gt;throw in some print statements since addmessage doesn't report anything?&lt;/P&gt;&lt;P&gt;In any event, if you are trying to figure out how to concatenate values from fixed length and potentially variable length inputs, throw them both into a list, add the lists and format accordingly&lt;/P&gt;&lt;LI-CODE lang="python"&gt;a0 = ["fixed length"]
a1 = [1, 2, 3]
("{} "*len(a0+a1)).format(*(a0+a1))
'fixed length 1 2 3 '&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 07 Jul 2021 00:04:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-way-to-add-values-to-a-string-for/m-p/1076144#M61589</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-07-07T00:04:12Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to add values to a string for UpdateCursor</title>
      <link>https://community.esri.com/t5/python-questions/best-way-to-add-values-to-a-string-for/m-p/1076322#M61606</link>
      <description>&lt;P&gt;Here's a blog you should read. It's got some techniques for doing stuff like this that I have found very useful.&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.esri.com/t5/python-blog/turbo-charging-data-manipulation-with-python-cursors-and/ba-p/884079" target="_blank" rel="noopener"&gt;Turbo Charging Data Manipulation with Python Curso... - Esri Community&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Basically you'll read your "look up" data into a dictionary and reference it by some id to get the value you want to write elsewhere. If I'm understanding your logic correctly, it would look something like this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# UpdateCursor to add all index grid cells for each label
gridList = {label: pageName for label, pageName in arcpy.da.SearchCursor(intersectOut, ("Label", "PageName"))}  # List from intersection
arcpy.AddMessage("Grid list = {}".format(gridList))  # For debugging
with arcpy.da.UpdateCursor(indexTable, ("Label", "PageName")) as uCursor:
    for label, pageName in uCursor:
        gridList_pageName = gridList.get(label)
        arcpy.AddMessage(gridList_pageName)
        # Before going to the next record in the site index table
        # write the PageName from gridList if there is one.
        if gridList_pageName:
            uCursor.updateRow((label, gridList_pageName))
        else:
            arcpy.AddWarning("Label {} not found in gridList; nothing updated.".format(label))&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 07 Jul 2021 15:48:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-way-to-add-values-to-a-string-for/m-p/1076322#M61606</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2021-07-07T15:48:22Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to add values to a string for UpdateCursor</title>
      <link>https://community.esri.com/t5/python-questions/best-way-to-add-values-to-a-string-for/m-p/1076369#M61609</link>
      <description>&lt;P&gt;You might get a Key Error in 6 if the label is not in the gridList.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;gridList_pageName = gridList.get(label, None)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;Will default the gridList_pageName to None if the key is not found and will continue execution.&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jul 2021 16:55:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-way-to-add-values-to-a-string-for/m-p/1076369#M61609</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-07-07T16:55:21Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to add values to a string for UpdateCursor</title>
      <link>https://community.esri.com/t5/python-questions/best-way-to-add-values-to-a-string-for/m-p/1076539#M61617</link>
      <description>&lt;P&gt;I like dict.get() because it returns &lt;FONT face="courier new,courier"&gt;None&lt;/FONT&gt; by default if the key is not found. You can specify a different value in the second argument but &lt;FONT face="courier new,courier"&gt;None&lt;/FONT&gt; is redundant.&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jul 2021 22:56:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-way-to-add-values-to-a-string-for/m-p/1076539#M61617</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2021-07-07T22:56:03Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to add values to a string for UpdateCursor</title>
      <link>https://community.esri.com/t5/python-questions/best-way-to-add-values-to-a-string-for/m-p/1076547#M61618</link>
      <description>Ah yes I forgot it does. Thanks!&lt;BR /&gt;</description>
      <pubDate>Wed, 07 Jul 2021 23:11:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-way-to-add-values-to-a-string-for/m-p/1076547#M61618</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-07-07T23:11:36Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to add values to a string for UpdateCursor</title>
      <link>https://community.esri.com/t5/python-questions/best-way-to-add-values-to-a-string-for/m-p/1076557#M61619</link>
      <description>&lt;P&gt;Thanks for looking at it! This works great, if there were only one index grid that each project falls in. Unfortunately, using a dictionary limits me to just one index grid for each project. The issue I am having is that there are several projects that fall into multiple grids.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I will keep plugging away at it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again!&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jul 2021 23:32:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-way-to-add-values-to-a-string-for/m-p/1076557#M61619</guid>
      <dc:creator>JamesWilcox1970</dc:creator>
      <dc:date>2021-07-07T23:32:03Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to add values to a string for UpdateCursor</title>
      <link>https://community.esri.com/t5/python-questions/best-way-to-add-values-to-a-string-for/m-p/1076591#M61622</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/429884"&gt;@JamesWilcox1970&lt;/a&gt;,&amp;nbsp;this is a new requirement from your initial post. Please describe all the rules in which these projects are grouped into index grids and we can help you find a solution.&lt;/P&gt;</description>
      <pubDate>Thu, 08 Jul 2021 03:27:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-way-to-add-values-to-a-string-for/m-p/1076591#M61622</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2021-07-08T03:27:13Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to add values to a string for UpdateCursor</title>
      <link>https://community.esri.com/t5/python-questions/best-way-to-add-values-to-a-string-for/m-p/1076796#M61631</link>
      <description>&lt;P&gt;I thought I had mentioned it in the original post, but I had not. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; I have a feature class generated by an Intersect between Projects and Index Grids. Many of the projects intersect multiple grids, so I have multiple records in the feature class for a single project, each with a different index grid cell. I am trying to programmatically populate a field in an output table with all the index grids that a particular project intersects. The result I am aiming for is "Project: Foo, Index Grid: A1, A2, B2; Project: Bar, Index Grid: A3, B1, C5."&lt;/P&gt;&lt;P&gt;So I am looking for a way to loop through the projects in the output table, find each index grid that matches that project in the Intersect feature class, write it to a string, and use that string in an UpdateCursor to update the index grid field (PageName) in the output table.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm sorry if I was unclear in my original post. Thanks for looking at this problem.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Jul 2021 16:19:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-way-to-add-values-to-a-string-for/m-p/1076796#M61631</guid>
      <dc:creator>JamesWilcox1970</dc:creator>
      <dc:date>2021-07-08T16:19:46Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to add values to a string for UpdateCursor</title>
      <link>https://community.esri.com/t5/python-questions/best-way-to-add-values-to-a-string-for/m-p/1077020#M61632</link>
      <description>&lt;P&gt;I may be mixing up projects and grids but I think you get the idea. Just do the spatial join, put it into a dictionary with project as key and the value is a list of intersecting grids. Then you can do what you need with it, like write it back to your feature class as a comma separated list of values.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from collections import defaultdict

try:
    # Perform spatial join between grid and project features.
    grid_project_spatialjoin_inmem = r"in_memory\grid_project_spatialjoin"
    arcpy.SpatialJoin_analysis(
        target_features=projectFeatures,
        join_features=gridFeatures,
        out_feature_class=grid_project_spatialjoin_inmem,
        join_operation="JOIN_ONE_TO_MANY"
    )
    # Create dictionary keys of projects and values with list of grids
    project_grids = defaultdict(list)
    with arcpy.da.SearchCursor(grid_project_spatialjoin_inmem, ("projectPageName", "gridLabel")) as sCursor:
        for projectPageName, gridLabel in sCursor:
            project_grids[projectPageName].append(gridLabel)
finally:
    arcpy.Delete_management("in_memory")

# Write the intersecting grids for each project as comma separated list
with arcpy.da.UpdateCursor(gridFeatures, ("projectPageName", "gridLabel")) as uCursor:
    for projectPageName, gridLabel in uCursor:
        grid_list = project_grids.get(projectPageName, "")
        uCursor.updateRow((projectPageName, ", ".join(grid_list)))&lt;/LI-CODE&gt;&lt;P&gt;EDIT: fixed a comment for creating the dictionary&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Jul 2021 14:04:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-way-to-add-values-to-a-string-for/m-p/1077020#M61632</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2021-07-09T14:04:14Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to add values to a string for UpdateCursor</title>
      <link>https://community.esri.com/t5/python-questions/best-way-to-add-values-to-a-string-for/m-p/1077036#M61633</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/191789"&gt;@BlakeTerhune&lt;/a&gt;&amp;nbsp;! It is going to take me a little bit to understand it, try it out and hammer out any bugs, but I have very high hopes now. I will update you as I go. Thanks again!&lt;/P&gt;</description>
      <pubDate>Thu, 08 Jul 2021 22:28:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-way-to-add-values-to-a-string-for/m-p/1077036#M61633</guid>
      <dc:creator>JamesWilcox1970</dc:creator>
      <dc:date>2021-07-08T22:28:51Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to add values to a string for UpdateCursor</title>
      <link>https://community.esri.com/t5/python-questions/best-way-to-add-values-to-a-string-for/m-p/1077411#M61667</link>
      <description>&lt;P&gt;Thank you,&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/191789"&gt;@BlakeTerhune&lt;/a&gt;&amp;nbsp;! I had to do some tweaking of the code to get it to work with the data, but your idea really helped. Here's the relevant portion of the working script:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def listToString(s):  # Takes input list and converts it to a string using join()
    # Empty string
    str1 = ""
    str1 = ", ".join(s)
    return str1

try:
    # Perform spatial join between grid and project features.
    grid_project_spatialjoin_inmem = r"in_memory\grid_project_spatialjoin"
    arcpy.SpatialJoin_analysis(
        target_features=culFC,
        join_features=quadFC,
        out_feature_class=grid_project_spatialjoin_inmem,
        join_operation="JOIN_ONE_TO_MANY"
    )
    arcpy.AddMessage("Join created.")
    # Create dictionary of grid keys and value with list of projects
    project_grids = defaultdict(list)
    with arcpy.da.SearchCursor(grid_project_spatialjoin_inmem, ("Label", "PageName")) as sCursor:
        for Label, PageName in sCursor:
            project_grids[Label].append(PageName)
finally:
    arcpy.Delete_management("in_memory")
    arcpy.AddMessage("Dictionary created.")
for key, value in project_grids.items():
    project_grids[key] = str(listToString(value))  # Changes the value to a string, but class is still list?

grid_list = project_grids.items()  # New dictionary from project_grids
grid_list = list(grid_list)  # Convert it to a list
# for item in grid_list:  # Checking list contents
#     arcpy.AddMessage("{}: {}.".format(item[0], item[1]))
with arcpy.da.InsertCursor(indexTable,("Label", "PageName")) as iCursor:
    for row in grid_list:
        iCursor.insertRow(row)
arcpy.AddMessage("Processing complete.")&lt;/LI-CODE&gt;&lt;P&gt;Thank you again for all the help!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Jim&lt;/P&gt;</description>
      <pubDate>Fri, 09 Jul 2021 20:01:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-way-to-add-values-to-a-string-for/m-p/1077411#M61667</guid>
      <dc:creator>JamesWilcox1970</dc:creator>
      <dc:date>2021-07-09T20:01:31Z</dc:date>
    </item>
  </channel>
</rss>

