<?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: Repeating Tuples with Insert/Search Cursor in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/repeating-tuples-with-insert-search-cursor/m-p/1675062#M75023</link>
    <description>&lt;P&gt;I keep getting an error, but want to confirm something first, when you define src_fields, where you have 'Plot_' that should be the corresponding field in "Forest_Surface_Fuels" to 'Plot_' in the out_fields, correct? I replaced 'Plot_' in those instances with 'Macroplot' and still get this error:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2025-12-28 093200.png" style="width: 648px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/146131i4F28562E2A1E129F/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot 2025-12-28 093200.png" alt="Screenshot 2025-12-28 093200.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Sun, 28 Dec 2025 16:32:37 GMT</pubDate>
    <dc:creator>KevinDickinson1</dc:creator>
    <dc:date>2025-12-28T16:32:37Z</dc:date>
    <item>
      <title>Repeating Tuples with Insert/Search Cursor</title>
      <link>https://community.esri.com/t5/python-questions/repeating-tuples-with-insert-search-cursor/m-p/1675026#M75021</link>
      <description>&lt;P&gt;I am trying to use an InsertCursor/SeachCursor to transfer data from one dataset to another wile incorporating a repeating tuple. The first dataset (in the below code is "&lt;STRONG&gt;Forest_Surface_Fuels&lt;/STRONG&gt;") contains all the data and has attribute fields for each record that contains data for 1hr, 10hr, and 100hr fuels. What I am trying to do as I transfer this data to a empty dataset ("&lt;STRONG&gt;1_10_100&lt;/STRONG&gt;") is to not only take the values in those fields, but create a master list for all records in a project and in a new field state whether the data is a 1hr, 10hr, or 100hr.&lt;/P&gt;&lt;P&gt;Here is the code that I have:&lt;/P&gt;&lt;P&gt;# Fields that data will be inserted into in the final excel table&lt;BR /&gt;fields = ['Plot_', 'SurveyType', 'Transect', 'OneHour', 'TenHr', 'HunHr']&lt;/P&gt;&lt;P&gt;#The data to be inserted (this is the tuple that will be repeated)&lt;BR /&gt;fuelsTuple = [&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ('Macroplot', "Pre", "1", 'onehr_pre_1', 'tenhr_pre_1', 'hundhr_pre_1'),&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ('Macroplot', "Pre", "2", 'onehr_pre_2', 'tenhr_pre_2', 'hundhr_pre_2'),&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ('Macroplot', "Post", "1", 'onehr_post_1', 'tenhr_post_1', 'hundhr_post_1'),&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ('Macroplot', "Post", "2", 'onehr_post_2', 'tenhr_post_2', 'hundhr_post_2')&lt;BR /&gt;]&lt;/P&gt;&lt;P&gt;#make count of times to repeat the tuple&lt;BR /&gt;repeats = arcpy.management.GetCount("Forest_Surface_Fuels")&lt;BR /&gt;count = int(repeats[0])&lt;BR /&gt;&lt;BR /&gt;#Insert Cursor to update fields&lt;BR /&gt;plotCursor = arcpy.da.InsertCursor("Forest_Surface_Fuels", fuelsTuple)&lt;BR /&gt;with arcpy.da.SearchCursor("1_10_100", fields) as cursor:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;for i in range(count):&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;for row in cursor:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;plotCursor.insertRow(row)&lt;BR /&gt;del plotCursor&lt;BR /&gt;del cursor&lt;/P&gt;&lt;P&gt;When I run this, I get this error:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2025-12-26 155449.png" style="width: 557px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/146128i10D32EB3CC53BCA8/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot 2025-12-26 155449.png" alt="Screenshot 2025-12-26 155449.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Fri, 26 Dec 2025 22:56:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/repeating-tuples-with-insert-search-cursor/m-p/1675026#M75021</guid>
      <dc:creator>KevinDickinson1</dc:creator>
      <dc:date>2025-12-26T22:56:40Z</dc:date>
    </item>
    <item>
      <title>Re: Repeating Tuples with Insert/Search Cursor</title>
      <link>https://community.esri.com/t5/python-questions/repeating-tuples-with-insert-search-cursor/m-p/1675037#M75022</link>
      <description>&lt;P&gt;You’re very close, but the cursors are flipped and you’re passing the &lt;STRONG&gt;tuple list as “fields”&lt;/STRONG&gt; (that’s why it blows up).&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;P&gt;&lt;STRONG&gt;SearchCursor&lt;/STRONG&gt; should read from &lt;STRONG&gt;Forest_Surface_Fuels&lt;/STRONG&gt; (source)&lt;/P&gt;&lt;/LI&gt;&lt;LI&gt;&lt;P&gt;&lt;STRONG&gt;InsertCursor&lt;/STRONG&gt; should write to &lt;STRONG&gt;1_10_100&lt;/STRONG&gt; (target)&lt;/P&gt;&lt;/LI&gt;&lt;LI&gt;&lt;P&gt;InsertCursor("Forest_Surface_Fuels", fuelsTuple) is wrong because the 2nd argument must be a &lt;STRONG&gt;list of field names&lt;/STRONG&gt;, not row data.&lt;/P&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;If your goal is: for each input feature, create &lt;STRONG&gt;4 rows&lt;/STRONG&gt; (Macroplot Pre/Post x Transect 1/2) and pull values from those named fields, do this pattern:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;out_fields = ['Plot_', 'SurveyType', 'Transect', 'OneHour', 'TenHr', 'HunHr']

templates = [
    ("Pre","1", "onehr_pre_1","tenhr_pre_1","hundhr_pre_1"),
    ("Pre","2", "onehr_pre_2","tenhr_pre_2","hundhr_pre_2"),
    ("Post","1","onehr_post_1","tenhr_post_1","hundhr_post_1"),
    ("Post","2","onehr_post_2","tenhr_post_2","hundhr_post_2"),
]

# read Plot_ plus all fuel fields you reference
src_fields = ['Plot_'] + sorted({f for t in templates for f in t[2:]})

with arcpy.da.SearchCursor("Forest_Surface_Fuels", src_fields) as sc, \
     arcpy.da.InsertCursor("1_10_100", out_fields) as ic:

    idx = {name:i for i,name in enumerate(src_fields)}

    for r in sc:
        plot_id = r[idx['Plot_']]
        for survey, tran, f1, f10, f100 in templates:
            ic.insertRow([plot_id, survey, tran, r[idx[f1]], r[idx[f10]], r[idx[f100]]])&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;That gives you the “master table” (one row per plot per survey/transect) with 1/10/100 hr values populated correctly.&lt;/P&gt;</description>
      <pubDate>Sat, 27 Dec 2025 03:43:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/repeating-tuples-with-insert-search-cursor/m-p/1675037#M75022</guid>
      <dc:creator>VenkataKondepati</dc:creator>
      <dc:date>2025-12-27T03:43:06Z</dc:date>
    </item>
    <item>
      <title>Re: Repeating Tuples with Insert/Search Cursor</title>
      <link>https://community.esri.com/t5/python-questions/repeating-tuples-with-insert-search-cursor/m-p/1675062#M75023</link>
      <description>&lt;P&gt;I keep getting an error, but want to confirm something first, when you define src_fields, where you have 'Plot_' that should be the corresponding field in "Forest_Surface_Fuels" to 'Plot_' in the out_fields, correct? I replaced 'Plot_' in those instances with 'Macroplot' and still get this error:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2025-12-28 093200.png" style="width: 648px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/146131i4F28562E2A1E129F/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot 2025-12-28 093200.png" alt="Screenshot 2025-12-28 093200.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 28 Dec 2025 16:32:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/repeating-tuples-with-insert-search-cursor/m-p/1675062#M75023</guid>
      <dc:creator>KevinDickinson1</dc:creator>
      <dc:date>2025-12-28T16:32:37Z</dc:date>
    </item>
    <item>
      <title>Re: Repeating Tuples with Insert/Search Cursor</title>
      <link>https://community.esri.com/t5/python-questions/repeating-tuples-with-insert-search-cursor/m-p/1675280#M75029</link>
      <description>&lt;P&gt;I'd approach this by making the field tuple a bit more complex. It seems that the first records are raw data and the last few are field values. You can just break it into two mappings like this:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# First Dictionary is direct data writes, second is field values to pull from the Origin dataset
conversion_map: list[tuple[dict[str, Any], dict[str, Any]]] = [
    (
        {'Plot_': 'Macroplot', 'SurveyType': 'Pre', 'Transect': '1'}, 
        {'OneHour': 'onehr_pre_1', 'TenHr': 'tenhr_pre_1', 'HunHr': 'hundhr_pre_1'}
    ),
    (
        {'Plot_': 'Macroplot', 'SurveyType': 'Pre', 'Transect': '2'}, 
        {'OneHour': 'onehr_pre_2', 'TenHr': 'tenhr_pre_2', 'HunHr': 'hundhr_pre_2'}
    ),
    (
        {'Plot_': 'Macroplot', 'SurveyType': 'Post', 'Transect':'1'}, 
        {'OneHour': 'onehr_post_1', 'TenHr': 'tenhr_post_1', 'HunHr': 'hundhr_post_1'}
    ),
    (
        {'Plot_': 'Macroplot', 'SurveyType': 'Post', 'Transect':'2'}, 
        {'OneHour': 'onehr_post_2', 'TenHr': 'tenhr_post_2', 'HunHr': 'hundhr_post_2'}
    ),
]&lt;/LI-CODE&gt;&lt;P&gt;That will make it clearer later what the raw data mapping is and what the extracted field values are.&lt;/P&gt;&lt;P&gt;When dealing with multiple cursors, I tend to like mapping the rows instead of using an index. This can be done with this incredibly simple helper function:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def as_dict(cur: SearchCursor) -&amp;gt; Iterator[dict[str, Any]]:
    for row in cur:
        yield dict(zip(cur.fields, row))&lt;/LI-CODE&gt;&lt;P&gt;You can wrap a cursor with this before iterating and you'll get back a dictionary that can be updated&lt;/P&gt;&lt;P&gt;&amp;nbsp;Now we need to implement the mapping:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def convert(origin: str, target: str, fields: list[str], conversion_map: ConversionMap, null_value: Any=None):
    with (
        SearchCursor(origin, fields) as s_cur,
        InsertCursor(target, fields) as i_cur
    ):
        for row in as_dict(s_cur):
            for fuel_data, fuel_fields in conversion_map:
                new_row = fuel_data.copy()
                new_row.update({k: row.get(v, null_value) for k, v in fuel_fields.items()})
                i_cur.insertRow([new_row.get(f, null_value) for f in i_cur.fields])&lt;/LI-CODE&gt;&lt;P&gt;Here we create a context block with two cursors, one to search through the origin table ("Forest_Surface_Fuels") and one to insert into the target table ("1_10_100").&amp;nbsp;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;(line: 2) Create a context block with 2 cursors for the Target and Origin&lt;/LI&gt;&lt;LI&gt;(line: 6) Iterate dictionary mappings of the origin rows&lt;/LI&gt;&lt;LI&gt;(line: 7) Iterate all defined row conversions&lt;/LI&gt;&lt;LI&gt;(line: 8 ) Create a copy of the template row data&lt;/LI&gt;&lt;LI&gt;(line: 9) Get the field value of the current origin row for each mapped target field&lt;/LI&gt;&lt;LI&gt;(line: 10) Create a row in the correct shape by iterating the insert fields and grabbing the new_row values&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;The null_value here shouldn't ever trigger, since it'll only exist if the fieldname doesn't exist in the origin table. If you don't have any 0 values, you can re-map nulls by using an or instead:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;new_row.update({k: row.get(v) or null_value for k, v in fuel_fields.items()})&lt;/LI-CODE&gt;&lt;P&gt;This abuses pythons truthiness, so any value that's considered falsey will get the null_value ('', 0, False, None, etc.)&lt;/P&gt;&lt;P&gt;Finally, we need to put all this together into a script that doesn't pollute the global namespace with cursor references. Basically just put your logic into an if __name__ == '__main__' block:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from collections.abc import Iterator
from typing import Any
from arcpy.da import SearchCursor, InsertCursor

ConversionMap = list[tuple[dict[str, Any], dict[str, Any]]]

def as_dict(cur: SearchCursor) -&amp;gt; Iterator[dict[str, Any]]:
    for row in cur:
        yield dict(zip(cur.fields, row))

def convert(origin: str, target: str, fields: list[str], conversion_map: ConversionMap, null_value: Any=None):
    with (
        SearchCursor(origin, fields) as s_cur,
        InsertCursor(target, fields) as i_cur
    ):
        for row in as_dict(s_cur):
            for fuel_data, fuel_fields in conversion_map:
                new_row = fuel_data.copy()
                new_row.update({k: row.get(v, null_value) for k, v in fuel_fields.items()})
                i_cur.insertRow([new_row.get(f, null_value) for f in i_cur.fields])

if __name__ == '__main__':
    # Fields that data will be inserted into in the final excel table
    FIELDS = ['Plot_', 'SurveyType', 'Transect', 'OneHour', 'TenHr', 'HunHr']
    
    ORIGIN_DATA = "Forest_Surface_Fuels"
    TARGET_DATA = "1_10_100"

    # Update this with the Null Value you want to use, e.g. ['' or -1 or None]
    NULL_VALUE = None

        # First Dictionary is direct data writes, second is field values to pull from the Origin dataset
    CONVERSION_MAP: ConversionMap = [
        (
            # Direct Data Insertion
            {'Plot_': 'Macroplot', 'SurveyType': 'Pre', 'Transect': '1'},
            # Field Value Map from Origin
            {'OneHour': 'onehr_pre_1', 'TenHr': 'tenhr_pre_1', 'HunHr': 'hundhr_pre_1'}
        ),
        (
            {'Plot_': 'Macroplot', 'SurveyType': 'Pre', 'Transect': '2'}, 
            {'OneHour': 'onehr_pre_2', 'TenHr': 'tenhr_pre_2', 'HunHr': 'hundhr_pre_2'}
        ),
        (
            {'Plot_': 'Macroplot', 'SurveyType': 'Post', 'Transect':'1'}, 
            {'OneHour': 'onehr_post_1', 'TenHr': 'tenhr_post_1', 'HunHr': 'hundhr_post_1'}
        ),
        (
            {'Plot_': 'Macroplot', 'SurveyType': 'Post', 'Transect':'2'}, 
            {'OneHour': 'onehr_post_2', 'TenHr': 'tenhr_post_2', 'HunHr': 'hundhr_post_2'}
        ),
    ]

    convert(ORIGIN_DATA, TARGET_DATA, FIELDS, CONVERSION_MAP, NULL_VALUE)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Dec 2025 15:16:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/repeating-tuples-with-insert-search-cursor/m-p/1675280#M75029</guid>
      <dc:creator>HaydenWelch</dc:creator>
      <dc:date>2025-12-30T15:16:26Z</dc:date>
    </item>
    <item>
      <title>Re: Repeating Tuples with Insert/Search Cursor</title>
      <link>https://community.esri.com/t5/python-questions/repeating-tuples-with-insert-search-cursor/m-p/1675292#M75031</link>
      <description>&lt;P&gt;Yes — src_fields must be the &lt;EM&gt;field names&lt;/EM&gt; in Forest_Surface_Fuels, not a value like "Macroplot". So don’t replace Plot_ with "Macroplot" in src_fields. "Macroplot" should be the value you insert, not a column name.&lt;/P&gt;&lt;P&gt;About the error you’re seeing:&lt;/P&gt;&lt;P&gt;RuntimeError: Invalid SQL syntax [near "PLOT": syntax error]&lt;/P&gt;&lt;P&gt;That usually happens when the target table has a field name that the underlying database treats as problematic (often a reserved word like PLOT, or a name that needs quoting). arcpy.da.InsertCursor builds an INSERT statement under the hood and can choke on that.&lt;/P&gt;&lt;P&gt;What to do:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;&lt;P&gt;Confirm the exact field name in the output table (not the alias). If it’s PLOT / Plot / Plot_, verify spelling/case.&lt;/P&gt;&lt;/LI&gt;&lt;LI&gt;&lt;P&gt;Rename the output field to something safe like PlotID (best fix).&lt;/P&gt;&lt;/LI&gt;&lt;LI&gt;&lt;P&gt;Re-run the cursor after renaming.&lt;/P&gt;&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;So: keep src_fields as real source column names, and fix the output field name that’s triggering SQL near PLOT.&lt;/P&gt;</description>
      <pubDate>Tue, 30 Dec 2025 16:23:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/repeating-tuples-with-insert-search-cursor/m-p/1675292#M75031</guid>
      <dc:creator>VenkataKondepati</dc:creator>
      <dc:date>2025-12-30T16:23:02Z</dc:date>
    </item>
    <item>
      <title>Re: Repeating Tuples with Insert/Search Cursor</title>
      <link>https://community.esri.com/t5/python-questions/repeating-tuples-with-insert-search-cursor/m-p/1675308#M75033</link>
      <description>&lt;P&gt;I ended up needing to convert Forest_Surface_Fuels&amp;nbsp; to a temporary layer for this to work, but it works as I hoped, so thank you!&lt;/P&gt;</description>
      <pubDate>Tue, 30 Dec 2025 17:15:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/repeating-tuples-with-insert-search-cursor/m-p/1675308#M75033</guid>
      <dc:creator>KevinDickinson1</dc:creator>
      <dc:date>2025-12-30T17:15:36Z</dc:date>
    </item>
  </channel>
</rss>

