<?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: dict(zip()) in for loop with Calc Field codeblock in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/dict-zip-in-for-loop-with-calc-field-codeblock/m-p/1336175#M68895</link>
    <description>&lt;P&gt;my line 6...&lt;/P&gt;&lt;UL class="lia-list-style-type-circle"&gt;&lt;LI&gt;if the id_val is a key in the dictionary, then pull its value and format it for output.&lt;UL&gt;&lt;LI&gt;I wasn't sure why you wanted ( ) brackets around the output, but I put them in, just remove them if not needed&lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;&lt;LI&gt;if it isn't, then return None (which will produce a &amp;lt;Null&amp;gt; in the table... this could be replaced with anything you want&lt;/LI&gt;&lt;/UL&gt;</description>
    <pubDate>Sun, 08 Oct 2023 22:42:29 GMT</pubDate>
    <dc:creator>DanPatterson</dc:creator>
    <dc:date>2023-10-08T22:42:29Z</dc:date>
    <item>
      <title>dict(zip()) in for loop with Calc Field codeblock</title>
      <link>https://community.esri.com/t5/python-questions/dict-zip-in-for-loop-with-calc-field-codeblock/m-p/1336160#M68886</link>
      <description>&lt;DIV&gt;&lt;P&gt;Good evening, some trouble with the below example code. This is my first time putting a loop in a function for Calculate Field. I've tried a number of ways (putting brackets after row in the condition of the for loop, putting a 0 inside the brackets, changing the equals sign, trying the brackets with Key, etc).&lt;/P&gt;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;KeyList = ['a','b','c']
ValueList = ['chair','lamp','rug']
Dict = dict(zip(KeyList,ValueList))​&lt;/LI-CODE&gt;&lt;LI-CODE lang="python"&gt;codeblock = """
def output(Key,Value):
    for row in KeyList:
        if row == Key:
            return Dict[KeyList]
        else:
            return Value"""​&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;TableExample:&lt;/P&gt;&lt;TABLE border="1" width="100%"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD width="33.333333333333336%" height="25px"&gt;&lt;P&gt;ID&lt;/P&gt;&lt;/TD&gt;&lt;TD width="33.333333333333336%" height="25px"&gt;NAME&lt;/TD&gt;&lt;TD width="33.333333333333336%" height="25px"&gt;&lt;STRONG&gt;&lt;FONT color="#993366"&gt;(NewField)&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="33.333333333333336%" height="40px"&gt;&lt;P&gt;a&lt;/P&gt;&lt;/TD&gt;&lt;TD width="33.333333333333336%" height="40px"&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD width="33.333333333333336%" height="40px"&gt;&lt;STRONG&gt;&lt;FONT color="#993366"&gt;(chair)&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD height="25px"&gt;z&lt;/TD&gt;&lt;TD height="25px"&gt;desk&lt;/TD&gt;&lt;TD height="25px"&gt;&lt;STRONG&gt;&lt;FONT color="#993366"&gt;(desk)&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="33.333333333333336%" height="25px"&gt;c&lt;/TD&gt;&lt;TD width="33.333333333333336%" height="25px"&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD width="33.333333333333336%" height="25px"&gt;&lt;STRONG&gt;&lt;FONT color="#993366"&gt;(rug)&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;arcpy.management.CalculateField(
    in_table = "TableExample",
    field = "NewField",
    expression = "output(!ID!,!NAME!)",
    expression_type = "PYTHON3",
    code_block = codeblock
    )​&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 08 Oct 2023 21:54:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/dict-zip-in-for-loop-with-calc-field-codeblock/m-p/1336160#M68886</guid>
      <dc:creator>coolflippers</dc:creator>
      <dc:date>2023-10-08T21:54:39Z</dc:date>
    </item>
    <item>
      <title>Re: dict(zip()) in for loop with Calc Field codeblock</title>
      <link>https://community.esri.com/t5/python-questions/dict-zip-in-for-loop-with-calc-field-codeblock/m-p/1336161#M68887</link>
      <description>&lt;P&gt;I am noticing something about this. I can get it to run, but only for the first row. In my scenario, I have a few hundred rows between ID cell 'a' and ID cell 'c'. I run my code, and NewField populates correctly based on the ID in row 1 and the NAME and row 2. So, my NewField, as applied to this example, is correct for its first two cells. However, on its third cell, which, in this example, should be 'rug', in my scenario in Arc it's blank. It's as if the function was satisfied on the first row, and then went to the else statement for all the remaining rows. hm. maybe i should do two for loops. let me try that&lt;/P&gt;</description>
      <pubDate>Sun, 08 Oct 2023 22:12:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/dict-zip-in-for-loop-with-calc-field-codeblock/m-p/1336161#M68887</guid>
      <dc:creator>coolflippers</dc:creator>
      <dc:date>2023-10-08T22:12:55Z</dc:date>
    </item>
    <item>
      <title>Re: dict(zip()) in for loop with Calc Field codeblock</title>
      <link>https://community.esri.com/t5/python-questions/dict-zip-in-for-loop-with-calc-field-codeblock/m-p/1336162#M68888</link>
      <description>&lt;P&gt;Symptom verified. I took out the else statement, and all the values in ValueList were calculated into the field. Now, I just need to figure out how to populate the remaining fields based off NAME without messing up my progress...&lt;/P&gt;</description>
      <pubDate>Sun, 08 Oct 2023 22:17:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/dict-zip-in-for-loop-with-calc-field-codeblock/m-p/1336162#M68888</guid>
      <dc:creator>coolflippers</dc:creator>
      <dc:date>2023-10-08T22:17:50Z</dc:date>
    </item>
    <item>
      <title>Re: dict(zip()) in for loop with Calc Field codeblock</title>
      <link>https://community.esri.com/t5/python-questions/dict-zip-in-for-loop-with-calc-field-codeblock/m-p/1336164#M68889</link>
      <description>&lt;P&gt;This is what my table looks like before calc field:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="coolflippers_0-1696803511818.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/82636i7AC549A347468C3E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="coolflippers_0-1696803511818.png" alt="coolflippers_0-1696803511818.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 08 Oct 2023 22:18:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/dict-zip-in-for-loop-with-calc-field-codeblock/m-p/1336164#M68889</guid>
      <dc:creator>coolflippers</dc:creator>
      <dc:date>2023-10-08T22:18:39Z</dc:date>
    </item>
    <item>
      <title>Re: dict(zip()) in for loop with Calc Field codeblock</title>
      <link>https://community.esri.com/t5/python-questions/dict-zip-in-for-loop-with-calc-field-codeblock/m-p/1336166#M68890</link>
      <description>&lt;P&gt;If I include the else statement, only field in the list populates correctly:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="coolflippers_1-1696803601333.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/82637iC0B3E60D8A4937E2/image-size/medium?v=v2&amp;amp;px=400" role="button" title="coolflippers_1-1696803601333.png" alt="coolflippers_1-1696803601333.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 08 Oct 2023 22:20:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/dict-zip-in-for-loop-with-calc-field-codeblock/m-p/1336166#M68890</guid>
      <dc:creator>coolflippers</dc:creator>
      <dc:date>2023-10-08T22:20:40Z</dc:date>
    </item>
    <item>
      <title>Re: dict(zip()) in for loop with Calc Field codeblock</title>
      <link>https://community.esri.com/t5/python-questions/dict-zip-in-for-loop-with-calc-field-codeblock/m-p/1336169#M68891</link>
      <description>&lt;P&gt;But, when I delete the 'else' statement, the new field populates correctly off the list (of course omitting all the remaining fields)&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="coolflippers_2-1696803703404.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/82638i8542D8E8F6608913/image-size/medium?v=v2&amp;amp;px=400" role="button" title="coolflippers_2-1696803703404.png" alt="coolflippers_2-1696803703404.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 08 Oct 2023 22:23:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/dict-zip-in-for-loop-with-calc-field-codeblock/m-p/1336169#M68891</guid>
      <dc:creator>coolflippers</dc:creator>
      <dc:date>2023-10-08T22:23:21Z</dc:date>
    </item>
    <item>
      <title>Re: dict(zip()) in for loop with Calc Field codeblock</title>
      <link>https://community.esri.com/t5/python-questions/dict-zip-in-for-loop-with-calc-field-codeblock/m-p/1336170#M68892</link>
      <description>&lt;P&gt;Is this what you are trying to do?&lt;/P&gt;&lt;LI-CODE lang="python"&gt;KeyList = ['a','b','c']
ValueList = ['chair','lamp','rug']
Dict = dict(zip(KeyList,ValueList))

def output(id_val, Dict):
    if id_val in Dict.keys():
        return (f"({Dict[id_val]})")
    else:
        return None
        

for i in ['a', 'z', 'c', 'd', 'a']:
    print(output(i, Dict))
    
(chair)
None
(rug)
None
(chair)&lt;/LI-CODE&gt;</description>
      <pubDate>Sun, 08 Oct 2023 22:28:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/dict-zip-in-for-loop-with-calc-field-codeblock/m-p/1336170#M68892</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2023-10-08T22:28:45Z</dc:date>
    </item>
    <item>
      <title>Re: dict(zip()) in for loop with Calc Field codeblock</title>
      <link>https://community.esri.com/t5/python-questions/dict-zip-in-for-loop-with-calc-field-codeblock/m-p/1336172#M68893</link>
      <description>&lt;LI-CODE lang="python"&gt;codeblock = """
def output(Key,Value):
    for row in KeyList:
        if row == Key:
            return Dict[KeyList]
     for row in KeyList:
        if row != Key:
            return Value"""&lt;/LI-CODE&gt;&lt;P&gt;This is my fix. Not that neat though. I feel like somebody probably has a better idea...&lt;/P&gt;</description>
      <pubDate>Sun, 08 Oct 2023 22:33:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/dict-zip-in-for-loop-with-calc-field-codeblock/m-p/1336172#M68893</guid>
      <dc:creator>coolflippers</dc:creator>
      <dc:date>2023-10-08T22:33:23Z</dc:date>
    </item>
    <item>
      <title>Re: dict(zip()) in for loop with Calc Field codeblock</title>
      <link>https://community.esri.com/t5/python-questions/dict-zip-in-for-loop-with-calc-field-codeblock/m-p/1336173#M68894</link>
      <description>&lt;P&gt;I'm not sure. I feel like what you posted is probably a better way to do it. I've never used the .keys function.&lt;/P&gt;</description>
      <pubDate>Sun, 08 Oct 2023 22:35:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/dict-zip-in-for-loop-with-calc-field-codeblock/m-p/1336173#M68894</guid>
      <dc:creator>coolflippers</dc:creator>
      <dc:date>2023-10-08T22:35:15Z</dc:date>
    </item>
    <item>
      <title>Re: dict(zip()) in for loop with Calc Field codeblock</title>
      <link>https://community.esri.com/t5/python-questions/dict-zip-in-for-loop-with-calc-field-codeblock/m-p/1336175#M68895</link>
      <description>&lt;P&gt;my line 6...&lt;/P&gt;&lt;UL class="lia-list-style-type-circle"&gt;&lt;LI&gt;if the id_val is a key in the dictionary, then pull its value and format it for output.&lt;UL&gt;&lt;LI&gt;I wasn't sure why you wanted ( ) brackets around the output, but I put them in, just remove them if not needed&lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;&lt;LI&gt;if it isn't, then return None (which will produce a &amp;lt;Null&amp;gt; in the table... this could be replaced with anything you want&lt;/LI&gt;&lt;/UL&gt;</description>
      <pubDate>Sun, 08 Oct 2023 22:42:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/dict-zip-in-for-loop-with-calc-field-codeblock/m-p/1336175#M68895</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2023-10-08T22:42:29Z</dc:date>
    </item>
    <item>
      <title>Re: dict(zip()) in for loop with Calc Field codeblock</title>
      <link>https://community.esri.com/t5/python-questions/dict-zip-in-for-loop-with-calc-field-codeblock/m-p/1336528#M68936</link>
      <description>&lt;P&gt;So, first thing: the dict(zip()) with two lists is cool, but dictionaries can also just be manually made if that's easier.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;keyDict = {a:red, b:blue, ...}
#Code&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The other thing that you can try is a dictionary &lt;A href="https://www.geeksforgeeks.org/python-dictionary-get-method/#" target="_blank" rel="noopener"&gt;Get()&lt;/A&gt; method. This will return the value from the dictionary if the key is there OR else return a default value specified in the second parameter of the method. If you leave the second parameter empty, it will default to &lt;STRONG&gt;None&lt;/STRONG&gt;, but you can also manually make it that way.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;keyDict = {"a":"red", "b":"blue", "c":"yellow"}
res = keyDict.get("a", "white")
print(res)
# red

res = keyDict.get("d", "white)
print(res)
# white

# The None does not have any quotes on purpose.
# It is truly no value, not the word "None".
res = keyDict.get("d", None)
print(res)
# None
res = keyDict.get("e")
print(res)
# None&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, give this a shot?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# I think I messed up with the actual function part here
# but the code inside is good.

def output():
    KeyList = ['a','b','c']
    ValueList = ['chair','lamp','rug']
    # (Don't name your variables after functions, including "dict")
    keyDict = dict(zip(KeyList,ValueList))

    for key in KeyList:
        return keyDict.get(key, None)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 10 Oct 2023 12:58:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/dict-zip-in-for-loop-with-calc-field-codeblock/m-p/1336528#M68936</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2023-10-10T12:58:06Z</dc:date>
    </item>
  </channel>
</rss>

