<?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: Cool cursor dictionary constructor one-liner in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/cool-cursor-dictionary-constructor-one-liner/m-p/468720#M36583</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have a feeling this is going to save me a ton of time some day. Well done.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 13 Mar 2012 13:55:18 GMT</pubDate>
    <dc:creator>PhilMorefield</dc:creator>
    <dc:date>2012-03-13T13:55:18Z</dc:date>
    <item>
      <title>Cool cursor dictionary constructor one-liner</title>
      <link>https://community.esri.com/t5/python-questions/cool-cursor-dictionary-constructor-one-liner/m-p/468716#M36579</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I bet someone else will think this is cool too.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In one line, turn your attribute table into a Python dictionary. Of course faster (and actually one line not 3) if you don't have to use .getValue!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Apparently there is a bit more memory overhead doing it this way, but hey...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;keyField = "OBJECTID"
valueField = "DIFFERENT_ID"
exampleDict = dict([(searchRow.getValue(keyField), searchRow.getValue(valueField)) for searchRow in arcpy.SearchCursor(myTable)])&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://www.python.org/dev/peps/pep-0274/" rel="nofollow noopener noreferrer" target="_blank"&gt;http://www.python.org/dev/peps/pep-0274/&lt;/A&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 20:46:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cool-cursor-dictionary-constructor-one-liner/m-p/468716#M36579</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2021-12-11T20:46:49Z</dc:date>
    </item>
    <item>
      <title>Re: Cool cursor dictionary constructor one-liner</title>
      <link>https://community.esri.com/t5/python-questions/cool-cursor-dictionary-constructor-one-liner/m-p/468717#M36580</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;or better yet...&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;exampleDict = dict([(r.KEY, (r.VALUE1, r.VALUE2)) for r in arcpy.SearchCursor(myTbl)])&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 08 Mar 2012 20:59:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cool-cursor-dictionary-constructor-one-liner/m-p/468717#M36580</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2012-03-08T20:59:44Z</dc:date>
    </item>
    <item>
      <title>Re: Cool cursor dictionary constructor one-liner</title>
      <link>https://community.esri.com/t5/python-questions/cool-cursor-dictionary-constructor-one-liner/m-p/468718#M36581</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I like the cut of your jib, I'll have to try implementing this somewhere.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 08 Mar 2012 21:18:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cool-cursor-dictionary-constructor-one-liner/m-p/468718#M36581</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2012-03-08T21:18:14Z</dc:date>
    </item>
    <item>
      <title>Re: Cool cursor dictionary constructor one-liner</title>
      <link>https://community.esri.com/t5/python-questions/cool-cursor-dictionary-constructor-one-liner/m-p/468719#M36582</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I like it, Chris! &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Useful and simple.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 12 Mar 2012 12:22:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cool-cursor-dictionary-constructor-one-liner/m-p/468719#M36582</guid>
      <dc:creator>RDHarles</dc:creator>
      <dc:date>2012-03-12T12:22:59Z</dc:date>
    </item>
    <item>
      <title>Re: Cool cursor dictionary constructor one-liner</title>
      <link>https://community.esri.com/t5/python-questions/cool-cursor-dictionary-constructor-one-liner/m-p/468720#M36583</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have a feeling this is going to save me a ton of time some day. Well done.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 13 Mar 2012 13:55:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cool-cursor-dictionary-constructor-one-liner/m-p/468720#M36583</guid>
      <dc:creator>PhilMorefield</dc:creator>
      <dc:date>2012-03-13T13:55:18Z</dc:date>
    </item>
    <item>
      <title>Re: Cool cursor dictionary constructor one-liner</title>
      <link>https://community.esri.com/t5/python-questions/cool-cursor-dictionary-constructor-one-liner/m-p/468721#M36584</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;or better yet...&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;exampleDict = dict([(r.KEY, (r.VALUE1, r.VALUE2)) for r in arcpy.SearchCursor(myTbl)])&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt;I've always liked this little piece of code, but a couple of things have always bothered me. The dictionary that results gives you the attributes of your 'KEY' as a series of values (r.VALUE1, r.VALUE2), but you lose the attribute name. Working with big datasets with many 'VALUE's could be difficult. Also, it requires that you go in and enter each field name you wanted included in the output dictionary. Again, with big datasets this may be problem.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This little ditty uses nested list comprehension to solve those problems:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# for this example, use the Census 2000 state shapefile
myShp = 'R:\\data\\Census\\2000\\Shape\\ST.shp'

myDict = dict([((r.ST_ABBREV, f.name), r.getValue(f.name)) for f in ap.ListFields(myShp) for r in ap.SearchCursor(myShp)])

&amp;gt;&amp;gt;&amp;gt; myDict[('WY', 'STATE_NAME')]
&amp;gt;&amp;gt;&amp;gt; 'Wyoming'
&amp;gt;&amp;gt;&amp;gt; myDict[('MD', 'ID')]
&amp;gt;&amp;gt;&amp;gt; '24'&amp;nbsp; #this is the FIPS code for Maryland&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;Even though calling this a 'one-liner' is a bit of stretch (it's 123 characters), this lets me access the entire shapefile attribute table, in this case, as a dictionary using a two-item tuple as the key.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;And, I don't need to know the names or number of fields in advance. All I have to do is provide the name of the field I want to serve as the key, which is ST_ABBREV in this case.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 20:46:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cool-cursor-dictionary-constructor-one-liner/m-p/468721#M36584</guid>
      <dc:creator>PhilMorefield</dc:creator>
      <dc:date>2021-12-11T20:46:51Z</dc:date>
    </item>
    <item>
      <title>Re: Cool cursor dictionary constructor one-liner</title>
      <link>https://community.esri.com/t5/python-questions/cool-cursor-dictionary-constructor-one-liner/m-p/468722#M36585</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Update: Dictionary comprehensions now suported in Python 2.7, so an updated syntax would look like:&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;valueDict = &lt;STRONG&gt;{&lt;/STRONG&gt;r[0]:(r[1],r[2]) for r in arcpy.da.SearchCursor(myFC, ["OID@","ID","NAME"])&lt;STRONG&gt;}&lt;/STRONG&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 31 Oct 2013 20:58:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cool-cursor-dictionary-constructor-one-liner/m-p/468722#M36585</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2013-10-31T20:58:58Z</dc:date>
    </item>
    <item>
      <title>Re: Cool cursor dictionary constructor one-liner</title>
      <link>https://community.esri.com/t5/python-questions/cool-cursor-dictionary-constructor-one-liner/m-p/468723#M36586</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Update: Dictionary comprehensions now suported in Python 2.7, so an updated syntax would look like:&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;valueDict = &lt;STRONG&gt;{&lt;/STRONG&gt;r[0]:(r[1],r[2]) for r in arcpy.da.SearchCursor(myFC, ["OID@","ID","NAME"])&lt;STRONG&gt;}&lt;/STRONG&gt;&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I use something like this to compare geometries by ID values. The above can also easily be a function (although its no longer one line)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
def makeFeatureDict(myFC, keyField, fields):
&amp;nbsp;&amp;nbsp;&amp;nbsp; cursorFields = [keyField] + fields
&amp;nbsp;&amp;nbsp;&amp;nbsp; valueDict = {r[0]:[v for v in r[1:]] for r in arcpy.da.SearchCursor(myFC, cursorFields)}
&amp;nbsp;&amp;nbsp;&amp;nbsp; del cursorFields
&amp;nbsp;&amp;nbsp;&amp;nbsp; return valueDict
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 20:46:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cool-cursor-dictionary-constructor-one-liner/m-p/468723#M36586</guid>
      <dc:creator>DouglasSands</dc:creator>
      <dc:date>2021-12-11T20:46:54Z</dc:date>
    </item>
    <item>
      <title>Re: Cool cursor dictionary constructor one-liner</title>
      <link>https://community.esri.com/t5/python-questions/cool-cursor-dictionary-constructor-one-liner/m-p/468724#M36587</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;How about just leaving the field values (aka the dictionary values) as a tuple? It'd be slightly faster and more memory efficient that way... Unless you need to change the values in place (have the field values be mutable), I'd go with a tuple.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def makeFeatureDict(myFC, keyField, fields):
&amp;nbsp;&amp;nbsp;&amp;nbsp; cursorFields = [keyField] + fields
&amp;nbsp;&amp;nbsp;&amp;nbsp; valueDict = {r[0]:r[1:] for r in arcpy.da.SearchCursor(myFC, cursorFields)}
&amp;nbsp;&amp;nbsp;&amp;nbsp; del cursorFields
&amp;nbsp;&amp;nbsp;&amp;nbsp; return valueDict&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Also, I was curious about performace and memory use metrics for this (tuple vs list), so I ran a little test:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;... 2.1 million records and 4 value fields (one as the key)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;... reporting on average time and RAM use for 3 runs of each method&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN style="text-decoration:underline;"&gt;tuple method&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;23 sec @404,000k memory use&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN style="text-decoration:underline;"&gt;list method&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;27 sec @455,000k memory use&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 20:46:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cool-cursor-dictionary-constructor-one-liner/m-p/468724#M36587</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2021-12-11T20:46:57Z</dc:date>
    </item>
    <item>
      <title>Re: Cool cursor dictionary constructor one-liner</title>
      <link>https://community.esri.com/t5/python-questions/cool-cursor-dictionary-constructor-one-liner/m-p/468725#M36588</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;How about just leaving the field values (aka the dictionary values) as a tuple?&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Yes. Was overthinking. But the whole thing can also be simplified to two lines:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
def makeFeatureDict(myFC, keyField, fields):
&amp;nbsp;&amp;nbsp;&amp;nbsp; return {r[0]:r[1:] for r in arcpy.da.SearchCursor(myFC, [keyField] + fields)}
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 20:46:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cool-cursor-dictionary-constructor-one-liner/m-p/468725#M36588</guid>
      <dc:creator>DouglasSands</dc:creator>
      <dc:date>2021-12-11T20:46:59Z</dc:date>
    </item>
  </channel>
</rss>

