<?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: Cursor Searching 2 tables and Updating a 3rd in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/cursor-searching-2-tables-and-updating-a-3rd/m-p/321714#M25053</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You are using da UpdateCursor for the 3rd table. So, modify the assignment code by row[index] = value:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;for row in cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp; row[0] = x.S_RANK + y.G_RANK
&amp;nbsp;&amp;nbsp;&amp;nbsp; row[1] = x.DESCRIPTION + " + " + y.DESCRIPTION 
&amp;nbsp;&amp;nbsp;&amp;nbsp; cursor.updateRow(row)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 15:16:36 GMT</pubDate>
    <dc:creator>NobbirAhmed</dc:creator>
    <dc:date>2021-12-11T15:16:36Z</dc:date>
    <item>
      <title>Cursor Searching 2 tables and Updating a 3rd</title>
      <link>https://community.esri.com/t5/python-questions/cursor-searching-2-tables-and-updating-a-3rd/m-p/321713#M25052</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hey everyone,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am trying to:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;OL&gt;&lt;BR /&gt;&lt;LI&gt;Set a search cursor through table 1 which contains 2 fields. S_RANK and DESCRIPTION&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;within the first loop, I am cursor searching through Table 2 which contains 2 fields. G_RANK and DESCRIPTION&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;Updating a cursor on table 3 which contains 2 fields. ANHIC_RANK and DESCRIPTION&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;&lt;/LI&gt;&lt;BR /&gt;&lt;/OL&gt;&lt;SPAN&gt;In the update cursor, I want to populate ANHIC_RANKING field in table 3 with a concatenation of S_RANK + G_RANK from the first. I want to update the DESCRIPTION field in table 3 with a concatenation of the 2 DESCRIPTION fields from the first 2 tables (i.e. DESCRIPTION + " + " + DESCRIPTION)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The print statements in the first 2 loops print out fine, it's the update cursor that doesn't seem to update. Any suggestions?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Mike&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;
import arcpy
from arcpy import env

env.workspace = r"Z:\IOR\2012\2012_Geodatabase_UTM\DOMAIN_CODE_TABLES.gdb"

for x in arcpy.SearchCursor("ANHIC_RANK_S"):
&amp;nbsp;&amp;nbsp;&amp;nbsp; print x.S_RANK
&amp;nbsp;&amp;nbsp;&amp;nbsp; print x.DESCRIPTION

&amp;nbsp;&amp;nbsp;&amp;nbsp; for y in arcpy.SearchCursor("ANHIC_RANK_G"):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print y.G_RANK
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print y.DESCRIPTION

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; with arcpy.da.UpdateCursor("ANHIC_RANKING_FINAL", ["ANHIC_RANKING", "DESCRIPTION"]) as cursor:

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in cursor:
&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.ANHIC_RANKING = x.S_RANK + y.G_RANK
&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.DESCRIPTION = x.DESCRIPTION + " + " + y.DESCRIPTION
&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; c.updateRow(row)

&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 14 Jun 2012 21:45:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cursor-searching-2-tables-and-updating-a-3rd/m-p/321713#M25052</guid>
      <dc:creator>MikeMacRae</dc:creator>
      <dc:date>2012-06-14T21:45:35Z</dc:date>
    </item>
    <item>
      <title>Re: Cursor Searching 2 tables and Updating a 3rd</title>
      <link>https://community.esri.com/t5/python-questions/cursor-searching-2-tables-and-updating-a-3rd/m-p/321714#M25053</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You are using da UpdateCursor for the 3rd table. So, modify the assignment code by row[index] = value:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;for row in cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp; row[0] = x.S_RANK + y.G_RANK
&amp;nbsp;&amp;nbsp;&amp;nbsp; row[1] = x.DESCRIPTION + " + " + y.DESCRIPTION 
&amp;nbsp;&amp;nbsp;&amp;nbsp; cursor.updateRow(row)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:16:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cursor-searching-2-tables-and-updating-a-3rd/m-p/321714#M25053</guid>
      <dc:creator>NobbirAhmed</dc:creator>
      <dc:date>2021-12-11T15:16:36Z</dc:date>
    </item>
    <item>
      <title>Re: Cursor Searching 2 tables and Updating a 3rd</title>
      <link>https://community.esri.com/t5/python-questions/cursor-searching-2-tables-and-updating-a-3rd/m-p/321715#M25054</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You are also nesting the cursors, so for every record in the first table you run a cursor through the whole of table two,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;and for every record in table two, each time it is run, you run through and update every record in the third table.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This seems a very odd thing to do.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It was easier to run multiple parallel cursors in Arc9, where one explicitly stepped each cursor forward (as one still does with an Insert Cursor).&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;In 10, where one itterates the cursor instead of stepping it (simpler, but in this case, less flexiable), things are trickier.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I suggest running the search cursors independantly first. Store the extracted values in a list or dictionary (as appropriate).&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Then run the Update Cursor. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;If using lists of output from the searches, also increment a counter and access the values from the list using the counter as a list index.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;If using a dictionary, access by key.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 15 Jun 2012 12:44:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cursor-searching-2-tables-and-updating-a-3rd/m-p/321715#M25054</guid>
      <dc:creator>markdenil</dc:creator>
      <dc:date>2012-06-15T12:44:10Z</dc:date>
    </item>
    <item>
      <title>Re: Cursor Searching 2 tables and Updating a 3rd</title>
      <link>https://community.esri.com/t5/python-questions/cursor-searching-2-tables-and-updating-a-3rd/m-p/321716#M25055</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Make two lists from the first two tables with SearchCursor.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Use Python's zip function to combine two lists - item by item. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Finally, iterate over the UpdateCursor and update using the values from the zipped list.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is my sample code for your case:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy

# setting workspace and then using the data name is better
s_rank_fc = r"path_to_your_data\s_rank.shp"
g_rank_fc = r"path_to_your_data\g_rank.shp"
anhic_fc = r"path_to_your_data\anhic.shp"

rows_s = arcpy.SearchCursor(s_rank_fc)
# use list comprehension to create a list of lists
# each item is a list of two values rank and description
s_rank_list = [[row_s.CLASS, row_s.CLASS_DESC] for row_s in rows_s]

rows_g = arcpy.SearchCursor(g_rank_fc)
# use list comprehension to create a list of lists
# each item is a list of two values rank and description
g_rank_list = [[row_g.CLASS, row_g.CLASS_DESC] for row_g in rows_g]

# zip two lists
s_g_list = zip(s_rank_list, g_rank_list)

# open an UpdateCursor
rows = arcpy.UpdateCursor(anhic_fc)

i = 0&amp;nbsp; # this index is used to extract values from the zipped list

for row in rows:

&amp;nbsp;&amp;nbsp;&amp;nbsp; s_rank_desc = s_g_list&lt;I&gt;[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp; s_rank = s_rank_desc[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp; s_desc = s_rank_desc[1]
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; g_rank_desc = s_g_list&lt;I&gt;[1]
&amp;nbsp;&amp;nbsp;&amp;nbsp; g_rank = g_rank_desc[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp; g_desc = g_rank_desc[1]
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; row.CLASS = s_rank + g_rank
&amp;nbsp;&amp;nbsp;&amp;nbsp; row.CLASS_DESC = s_desc + "; " + g_desc
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; i += 1
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
del row
del rows&lt;/I&gt;&lt;/I&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:16:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cursor-searching-2-tables-and-updating-a-3rd/m-p/321716#M25055</guid>
      <dc:creator>NobbirAhmed</dc:creator>
      <dc:date>2021-12-11T15:16:39Z</dc:date>
    </item>
    <item>
      <title>Re: Cursor Searching 2 tables and Updating a 3rd</title>
      <link>https://community.esri.com/t5/python-questions/cursor-searching-2-tables-and-updating-a-3rd/m-p/321717#M25056</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks people,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;With your suggestions and modifications, I was able to crack the code I needed. I ended up creating 2 lists and updated the final table that way. I believe the reason why my table wouldn't update is because I created it from scratch and it didn't have any rows. I had to insert the rows first and then update them. I have a feeling if I tested the insert cursor and used that to update as I add rows, instead of using a second step with the update cursor, that would probably work too. I'll post for anyone interested.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Oh and thanks to Nobbir for introducing me to the 'zip' function. It was very useful!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
from arcpy import env

env.workspace = r"Z:\IOR\2012\2012_Geodatabase_UTM\DOMAIN_CODE_TABLES.gdb"

rankList = []
descList = []
z = 1

# set a search cursor to create every combination of x + y for both the RANK fields and the DESCRIPTION fields and build a list for each
for x in arcpy.SearchCursor("ANHIC_RANK_S"):

&amp;nbsp;&amp;nbsp;&amp;nbsp; for y in arcpy.SearchCursor("ANHIC_RANK_G"):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rankList.append(x.S_RANK + y.G_RANK)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; descList.append(x.DESCRIPTION + " + " + y.DESCRIPTION)

# Open an insert cursor to insert the correct number of rows in the new table
insertCursor = arcpy.InsertCursor("ANHIC_RANKING_FINAL")
z = 1

while z &amp;lt;= len(rankList):
&amp;nbsp;&amp;nbsp;&amp;nbsp; row = insertCursor.newRow()
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.insertRow(row)
&amp;nbsp;&amp;nbsp;&amp;nbsp; z +=1


# zip each list
rank = zip(rankDict)
desc = zip(descDict)

# open an UpdateCursor on the final table
rows = arcpy.UpdateCursor("ANHIC_RANKING_FINAL")

i = 0&amp;nbsp; # this index is used to extract values from the zipped list

for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; row.ANHIC_RANKING = rank&lt;I&gt;[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp; row.DESCRIPTION = desc&lt;I&gt;[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; i += 1
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
del row
del rows&lt;/I&gt;&lt;/I&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:16:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cursor-searching-2-tables-and-updating-a-3rd/m-p/321717#M25056</guid>
      <dc:creator>MikeMacRae</dc:creator>
      <dc:date>2021-12-11T15:16:42Z</dc:date>
    </item>
  </channel>
</rss>

