<?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: Iteration through Dictionaries with Lists/Tuples as stored values in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/iteration-through-dictionaries-with-lists-tuples/m-p/734260#M56920</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;That iteritems has tripped me up more than once...&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 19 May 2015 13:11:52 GMT</pubDate>
    <dc:creator>Zeke</dc:creator>
    <dc:date>2015-05-19T13:11:52Z</dc:date>
    <item>
      <title>Iteration through Dictionaries with Lists/Tuples as stored values</title>
      <link>https://community.esri.com/t5/python-questions/iteration-through-dictionaries-with-lists-tuples/m-p/734257#M56917</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I currently have a nested cursor that I need to convert to one Cursor and one dictionary (or series of dictionaries) to improve performance and get a more desirable output.&amp;nbsp; I am a novice at Python coding and I am trying to understand Dictionaries. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I found that I can store a list as a dictionary value and I can call the individual entry in the value list.&amp;nbsp; For example, if D is my dictionary with keys key1 through key15 and each key is associated with a list or tuple of 5 entries for its corresponding value.&amp;nbsp; (key12 : [Name, 10, 25, 0, ABCD]).&amp;nbsp; Maybe that notation isn't 100% correct but you get the idea, hopefully.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I can call the individual entry in the list by D["key12"][4] which will return ABCD.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have found problems when trying to iterate through these kinds of dictionaries though.&amp;nbsp; Is there a way to access the individual entries in the list/tuple during iteration or am I better off with 5 different dictionaries--one for each category in the list?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I actually have to step away from the computer so it may be a bit before I can get back to answer any questions.&amp;nbsp; I apologize. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Edit: Grammar.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 18 May 2015 15:23:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/iteration-through-dictionaries-with-lists-tuples/m-p/734257#M56917</guid>
      <dc:creator>RachaelJohnson</dc:creator>
      <dc:date>2015-05-18T15:23:39Z</dc:date>
    </item>
    <item>
      <title>Re: Iteration through Dictionaries with Lists/Tuples as stored values</title>
      <link>https://community.esri.com/t5/python-questions/iteration-through-dictionaries-with-lists-tuples/m-p/734258#M56918</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Please post the script you have so far. What are the "problems" you have trying top iterate? &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 18 May 2015 16:32:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/iteration-through-dictionaries-with-lists-tuples/m-p/734258#M56918</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2015-05-18T16:32:56Z</dc:date>
    </item>
    <item>
      <title>Re: Iteration through Dictionaries with Lists/Tuples as stored values</title>
      <link>https://community.esri.com/t5/python-questions/iteration-through-dictionaries-with-lists-tuples/m-p/734259#M56919</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I figured it out in the mean time, but thank you for your response nonetheless! In case any one else new is having the same problem I am:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This is how I built the dictionary with the lists as values:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;SPAN style="color: #808080; font-style: italic;"&gt;# Convert Excel constraint file to GIS table
&lt;/SPAN&gt;compare = arcpy.ExcelToTable_conversion(CRIT, ProjFolder + &lt;SPAN style="color: #008000; font-weight: bold;"&gt;r"\constraints"&lt;/SPAN&gt;)
fields = [&lt;SPAN style="color: #008000; font-weight: bold;"&gt;"Rowid"&lt;/SPAN&gt;, &lt;SPAN style="color: #008000; font-weight: bold;"&gt;"CODE"&lt;/SPAN&gt;, &lt;SPAN style="color: #008000; font-weight: bold;"&gt;"SOIL"&lt;/SPAN&gt;, &lt;SPAN style="color: #008000; font-weight: bold;"&gt;"MAX_SLOPE"&lt;/SPAN&gt;, &lt;SPAN style="color: #008000; font-weight: bold;"&gt;"MIN_CDA"&lt;/SPAN&gt;, &lt;SPAN style="color: #008000; font-weight: bold;"&gt;"MAX_CDA"&lt;/SPAN&gt;, &lt;SPAN style="color: #008000; font-weight: bold;"&gt;"WT_SEP"&lt;/SPAN&gt;, &lt;SPAN style="color: #008000; font-weight: bold;"&gt;"WT_RELAX"&lt;/SPAN&gt;,
&amp;nbsp; &lt;SPAN style="color: #008000; font-weight: bold;"&gt;"COAST_SEP"&lt;/SPAN&gt;, &lt;SPAN style="color: #008000; font-weight: bold;"&gt;"MIN_DEPTH"&lt;/SPAN&gt;, &lt;SPAN style="color: #008000; font-weight: bold;"&gt;"DEPTH_RELA"&lt;/SPAN&gt;, &lt;SPAN style="color: #008000; font-weight: bold;"&gt;"COAST_MIN_"&lt;/SPAN&gt;, &lt;SPAN style="color: #008000; font-weight: bold;"&gt;"BMP"&lt;/SPAN&gt;, &lt;SPAN style="color: #008000; font-weight: bold;"&gt;"MOD"&lt;/SPAN&gt;]
Fields = [f.name &lt;SPAN style="color: #000080; font-weight: bold;"&gt;for &lt;/SPAN&gt;f &lt;SPAN style="color: #000080; font-weight: bold;"&gt;in &lt;/SPAN&gt;arcpy.ListFields(compare)]
&lt;SPAN style="color: #808080; font-style: italic;"&gt;# Create dictionary from criteria table
&lt;/SPAN&gt;D = {r[&lt;SPAN style="color: #0000ff;"&gt;1&lt;/SPAN&gt;]:(r[2:]) &lt;SPAN style="color: #000080; font-weight: bold;"&gt;for &lt;/SPAN&gt;r &lt;SPAN style="color: #000080; font-weight: bold;"&gt;in &lt;/SPAN&gt;arcpy.da.SearchCursor(compare, Fields)}&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The problems I was having involved trying to call the individual entries from the value list.&amp;nbsp; The first problem I had was with the following: &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;for r in &lt;span class="lia-unicode-emoji" title=":anguished_face:"&gt;😧&lt;/span&gt;&lt;/P&gt;&lt;P&gt;if "A" not in r[0]:&lt;/P&gt;&lt;P&gt;print r&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It would only print the key.&amp;nbsp; Print r[0] printed the first letter.&amp;nbsp; I couldn't seem to access the value, only the key.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But I found out that it's because "for r in D" is the same as "for r in D.keys()", so it was only looking at the keys.&amp;nbsp; So when I loop, I have to do "for k, v in D.iteritems()" or just D.items() depending on the version I'm using.&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The following code accesses the individual items stored in the value lists:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;SPAN style="color: #000080; font-weight: bold;"&gt;for &lt;/SPAN&gt;k, v &lt;SPAN style="color: #000080; font-weight: bold;"&gt;in &lt;/SPAN&gt;D.iteritems():
&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #000080; font-weight: bold;"&gt;if &lt;/SPAN&gt;&lt;SPAN style="color: #008000; font-weight: bold;"&gt;"A" &lt;/SPAN&gt;&lt;SPAN style="color: #000080; font-weight: bold;"&gt;not in &lt;/SPAN&gt;v[&lt;SPAN style="color: #0000ff;"&gt;6&lt;/SPAN&gt;]:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #000080; font-weight: bold;"&gt;print &lt;/SPAN&gt;v[&lt;SPAN style="color: #0000ff;"&gt;6&lt;/SPAN&gt;]&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 07:18:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/iteration-through-dictionaries-with-lists-tuples/m-p/734259#M56919</guid>
      <dc:creator>RachaelJohnson</dc:creator>
      <dc:date>2021-12-12T07:18:27Z</dc:date>
    </item>
    <item>
      <title>Re: Iteration through Dictionaries with Lists/Tuples as stored values</title>
      <link>https://community.esri.com/t5/python-questions/iteration-through-dictionaries-with-lists-tuples/m-p/734260#M56920</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;That iteritems has tripped me up more than once...&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 May 2015 13:11:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/iteration-through-dictionaries-with-lists-tuples/m-p/734260#M56920</guid>
      <dc:creator>Zeke</dc:creator>
      <dc:date>2015-05-19T13:11:52Z</dc:date>
    </item>
  </channel>
</rss>

