<?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: Row objects in the Data Access module in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/row-objects-in-the-data-access-module/m-p/106373#M8214</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Maybe there will be someone from the Arcpy team at SERUG this year I can bug about it :D. It was obviously a deliberate choice to make such a big change. Ill use your method for now Chris. tuple.index is a good solution.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Performance is the big reason.&amp;nbsp; Getting the row back in a tuple that you can pull values by index is a much faster approach than creating a row object and then have to make individual calls to get specific values.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The approach of forming a dictionary and then pulling values out from that is okay, but that is adding overhead just to enhance readability.&amp;nbsp; I would much rather just add code comment(s) if I need to clarify which field value is which.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;-Dave&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 13 Nov 2012 18:55:37 GMT</pubDate>
    <dc:creator>DavidWynne</dc:creator>
    <dc:date>2012-11-13T18:55:37Z</dc:date>
    <item>
      <title>Row objects in the Data Access module</title>
      <link>https://community.esri.com/t5/python-questions/row-objects-in-the-data-access-module/m-p/106362#M8203</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Can anyone explain the reasoning behind returning a row object as a tuple when using the Data Access module update cursor? I can understand it with a search cursor, you can dict(zip(cursor.fields,row)) and make a dictionary that lets you call the values by field name. I don't understand how I should be using this without counting to find the field index number in the tuple.&amp;nbsp; The help docs are not very clear on this topic. It seems like returning a dictionary would be easier for the users. I only just was given permission to update to 10.1 because sp1 is now&amp;nbsp; out so I may have missed this discussion at an earlier date. So as not to be entirely contrary, I do like the with-as usage for cursor's. That's pretty neat.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 06 Nov 2012 19:15:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/row-objects-in-the-data-access-module/m-p/106362#M8203</guid>
      <dc:creator>ChrisMathers</dc:creator>
      <dc:date>2012-11-06T19:15:50Z</dc:date>
    </item>
    <item>
      <title>Re: Row objects in the Data Access module</title>
      <link>https://community.esri.com/t5/python-questions/row-objects-in-the-data-access-module/m-p/106363#M8204</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Just thought of this, it should work.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
with arcpy.da.UpdateCursor(FEATURE CLASS,'*') as cursor:
&amp;nbsp; rowDict=dict(zip(cursor.fields,xrange(len(cursor.fields)+1)))) # zip fields to range of list length
&amp;nbsp; for row in cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp; row[rowDict[FIELD NAME]] = VALUE # use rowDict to find field list index
&amp;nbsp; cursor.updateRow(row)
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 06:28:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/row-objects-in-the-data-access-module/m-p/106363#M8204</guid>
      <dc:creator>ChrisMathers</dc:creator>
      <dc:date>2021-12-11T06:28:15Z</dc:date>
    </item>
    <item>
      <title>Re: Row objects in the Data Access module</title>
      <link>https://community.esri.com/t5/python-questions/row-objects-in-the-data-access-module/m-p/106364#M8205</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;In the interest of learning new ways to skin cats:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The enumerate() function is a good way of looping over iterables while counting how far along you are. Also, don't forget 10.1 uses Python 2.7, so you can use dictionary comprehensions to get rid of the ubiquitous dict(zip()) calls. So you could reduce it to:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;fieldDict = {field: index for index, field in enumerate(cursor.fields)}&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Barely a change, really, but a bit cleaner.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 08 Nov 2012 01:28:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/row-objects-in-the-data-access-module/m-p/106364#M8205</guid>
      <dc:creator>ThomMackey</dc:creator>
      <dc:date>2012-11-08T01:28:28Z</dc:date>
    </item>
    <item>
      <title>Re: Row objects in the Data Access module</title>
      <link>https://community.esri.com/t5/python-questions/row-objects-in-the-data-access-module/m-p/106365#M8206</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;That is cleaner. Thanks Thom.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;EDIT: Interestingly, if you want to make a look up table (which I prefer to arrays if you dont need to do complex math) you can nest dictionary comprehensions.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
fields={field : index for index,field in enumerate(cursor.fields)}
LookUp={row[fields['ORIG_FID']] : {field : row[fields[field]] for field in fields} for row in cursor}
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 06:28:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/row-objects-in-the-data-access-module/m-p/106365#M8206</guid>
      <dc:creator>ChrisMathers</dc:creator>
      <dc:date>2021-12-11T06:28:17Z</dc:date>
    </item>
    <item>
      <title>Re: Row objects in the Data Access module</title>
      <link>https://community.esri.com/t5/python-questions/row-objects-in-the-data-access-module/m-p/106366#M8207</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You can... but should you? &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt; Don't forget the old axiom - it's twice as hard to debug code as it is to write it; so if you write code as cleverly as you can, you are by definition not smart enough to debug it... &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Just kidding, that's actually a really nice, concise way of dumping a feature class into a dict. Between that and the with... context, arcpy is really starting to feel Pythonic!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 08 Nov 2012 22:21:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/row-objects-in-the-data-access-module/m-p/106366#M8207</guid>
      <dc:creator>ThomMackey</dc:creator>
      <dc:date>2012-11-08T22:21:11Z</dc:date>
    </item>
    <item>
      <title>Re: Row objects in the Data Access module</title>
      <link>https://community.esri.com/t5/python-questions/row-objects-in-the-data-access-module/m-p/106367#M8208</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;A really slick way that I use to return rows as dicts is:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
def rows_as_dicts(cursor):
&amp;nbsp;&amp;nbsp;&amp;nbsp; colnames = cursor.fields
&amp;nbsp;&amp;nbsp;&amp;nbsp; uc = hasattr(cursor, 'updateRow')
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row_object = dict(zip(colnames, row))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; yield row_object
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if uc:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cursor.updateRow([row_object[colname] for colname in colnames])

&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Use this generator function like so:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
with arcpy.da.SearchCursor(fc, ['*']) as sc:
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows_as_dicts(sc):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; x = row['MAPNAME']
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; blah ...
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This works with both da.SearchCursors and UpdateCursors.&amp;nbsp; In the later case, it will update each row.&amp;nbsp; I got the idea for this from a posting on Cafe Python and modified it to be able to determine which type of cursor is passed to the generator.&amp;nbsp; Excellent idea from Jason or one other ESRI Python guys.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Mike&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 06:28:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/row-objects-in-the-data-access-module/m-p/106367#M8208</guid>
      <dc:creator>MikeHunter</dc:creator>
      <dc:date>2021-12-11T06:28:20Z</dc:date>
    </item>
    <item>
      <title>Re: Row objects in the Data Access module</title>
      <link>https://community.esri.com/t5/python-questions/row-objects-in-the-data-access-module/m-p/106368#M8209</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Use the .fields property (a property of the .da cursor). It gives you a tuple of the field names returned by the cursor... Then use the .index property of the .fields tuple. For example:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;updateRows = arcpy.da.UpdateCursor(myTable,["*"])
for updateRow in updateRows:
&amp;nbsp;&amp;nbsp; updateRow[updateRows.fields.index("BASIN_NAME")] = "Basin "+ str(updateRow[updateRows.fields.index("BASIN_ID")])&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I found that using the .fields property like this is no slower (well maybe a teeny bit) than just using the index to set/retreive the field value... even for millions of records. Like less than a second differance...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In my opinion, the .da cursors are the best thing since slided bread.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 06:28:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/row-objects-in-the-data-access-module/m-p/106368#M8209</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2021-12-11T06:28:23Z</dc:date>
    </item>
    <item>
      <title>Re: Row objects in the Data Access module</title>
      <link>https://community.esri.com/t5/python-questions/row-objects-in-the-data-access-module/m-p/106369#M8210</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Speed wise they are glorious. I just dont get why we had attribute access changed to a tuple from a property. Never used the index method of a tuple before though, certainly looks nicer than what I had in place.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 13 Nov 2012 16:25:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/row-objects-in-the-data-access-module/m-p/106369#M8210</guid>
      <dc:creator>ChrisMathers</dc:creator>
      <dc:date>2012-11-13T16:25:50Z</dc:date>
    </item>
    <item>
      <title>Re: Row objects in the Data Access module</title>
      <link>https://community.esri.com/t5/python-questions/row-objects-in-the-data-access-module/m-p/106370#M8211</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Yep - I had the same complaint when I started messing around with the da cursors: What happened to .getValue and .setValue? How are you supposed to reference the field names?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;updateRow[updateRows.fields.index("FIELD_NAME")])&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;was the best replacement I could come up with... Maybe a bit longer code wise, but that's okay. Maybe it'd be better to have a little function fetch the index, but... &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Why a tuple? I don't know, but I have found it to be quite nice for a lot of things, such as direct table to dictionary conversion and data conversion. It's nice to have the row values be a an iterable data type. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Took me a while to appreciate it (just like the v10.0 MapAlgebra syntax), but now that I get it, I like it way better.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 13 Nov 2012 16:37:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/row-objects-in-the-data-access-module/m-p/106370#M8211</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2012-11-13T16:37:33Z</dc:date>
    </item>
    <item>
      <title>Re: Row objects in the Data Access module</title>
      <link>https://community.esri.com/t5/python-questions/row-objects-in-the-data-access-module/m-p/106371#M8212</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Maybe there will be someone from the Arcpy team at SERUG this year I can bug about it :D. It was obviously a deliberate choice to make such a big change. Ill use your method for now Chris. tuple.index is a good solution.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 13 Nov 2012 16:47:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/row-objects-in-the-data-access-module/m-p/106371#M8212</guid>
      <dc:creator>ChrisMathers</dc:creator>
      <dc:date>2012-11-13T16:47:42Z</dc:date>
    </item>
    <item>
      <title>Re: Row objects in the Data Access module</title>
      <link>https://community.esri.com/t5/python-questions/row-objects-in-the-data-access-module/m-p/106372#M8213</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;BTW: I have heard that the .da cursors are similar to the way the ArcObjects cursors behave (with an index).&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 13 Nov 2012 17:39:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/row-objects-in-the-data-access-module/m-p/106372#M8213</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2012-11-13T17:39:27Z</dc:date>
    </item>
    <item>
      <title>Re: Row objects in the Data Access module</title>
      <link>https://community.esri.com/t5/python-questions/row-objects-in-the-data-access-module/m-p/106373#M8214</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Maybe there will be someone from the Arcpy team at SERUG this year I can bug about it :D. It was obviously a deliberate choice to make such a big change. Ill use your method for now Chris. tuple.index is a good solution.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Performance is the big reason.&amp;nbsp; Getting the row back in a tuple that you can pull values by index is a much faster approach than creating a row object and then have to make individual calls to get specific values.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The approach of forming a dictionary and then pulling values out from that is okay, but that is adding overhead just to enhance readability.&amp;nbsp; I would much rather just add code comment(s) if I need to clarify which field value is which.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;-Dave&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 13 Nov 2012 18:55:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/row-objects-in-the-data-access-module/m-p/106373#M8214</guid>
      <dc:creator>DavidWynne</dc:creator>
      <dc:date>2012-11-13T18:55:37Z</dc:date>
    </item>
  </channel>
</rss>

