<?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: Using dictionary in field calculator in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/using-dictionary-in-field-calculator/m-p/1368830#M69625</link>
    <description>&lt;P&gt;Thanks for the prompt reply &lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/215600"&gt;@DanPatterson&lt;/a&gt;. Rather than replace the key "ABUT THE" with the value "Velvet leaf" the code keeps the field values the same.&lt;/P&gt;</description>
    <pubDate>Thu, 11 Jan 2024 12:17:18 GMT</pubDate>
    <dc:creator>SSISC_FieldCrew</dc:creator>
    <dc:date>2024-01-11T12:17:18Z</dc:date>
    <item>
      <title>Using dictionary in field calculator</title>
      <link>https://community.esri.com/t5/python-questions/using-dictionary-in-field-calculator/m-p/1368787#M69618</link>
      <description>&lt;P&gt;I am working with the field calculator in ArcPro. I have created a dictionary where the Key is an existing value in a field and the value is the one I would like to replace the existing value with. Below is a portion of my dictionary.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;plant_dictionary = {
    "ABUT THE": "Velvet leaf",
    "ACRO REP": "Russian knapweed",
    "AEGI CYL": "Jointed goatgrass",
    "AEGO POD": "Goutweed / bishop's weed",
    "AILA ALT": "Tree of heaven",
    "ALHA MAU": "Camel thorn",
    "ALLI PET": "Garlic mustard"
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;then I have this bit of code where INV_SP_PLA is the field whose data I am trying to replace.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;for r in plant_dictionary:
    if !INV_SP_PLA! in r:
        !INV_SP_PLA!.replace(r, plant_dictionary[r])
    else:
        pass&lt;/LI-CODE&gt;&lt;P&gt;ArcPro does not like my dictionary for some reason and I am unsure of what to put in the Expression portion of the field calculator.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any ideas why this might not be working?&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jan 2024 09:21:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-dictionary-in-field-calculator/m-p/1368787#M69618</guid>
      <dc:creator>SSISC_FieldCrew</dc:creator>
      <dc:date>2024-01-11T09:21:04Z</dc:date>
    </item>
    <item>
      <title>Re: Using dictionary in field calculator</title>
      <link>https://community.esri.com/t5/python-questions/using-dictionary-in-field-calculator/m-p/1368814#M69620</link>
      <description>&lt;P&gt;expression&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;plant(!INV_SP_PLA!)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;code block&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def plant(val):
    """Demo"""
    plant_dictionary = {
        "ABUT THE": "Velvet leaf",
        "ACRO REP": "Russian knapweed",
        "AEGI CYL": "Jointed goatgrass",
        "AEGO POD": "Goutweed / bishop's weed",
        "AILA ALT": "Tree of heaven",
        "ALHA MAU": "Camel thorn",
        "ALLI PET": "Garlic mustard"
    }
    for r in plant_dictionary:
        if val in r:
            val.replace(r, plant_dictionary[r])
        return val&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jan 2024 15:43:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-dictionary-in-field-calculator/m-p/1368814#M69620</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2024-01-11T15:43:39Z</dc:date>
    </item>
    <item>
      <title>Re: Using dictionary in field calculator</title>
      <link>https://community.esri.com/t5/python-questions/using-dictionary-in-field-calculator/m-p/1368830#M69625</link>
      <description>&lt;P&gt;Thanks for the prompt reply &lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/215600"&gt;@DanPatterson&lt;/a&gt;. Rather than replace the key "ABUT THE" with the value "Velvet leaf" the code keeps the field values the same.&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jan 2024 12:17:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-dictionary-in-field-calculator/m-p/1368830#M69625</guid>
      <dc:creator>SSISC_FieldCrew</dc:creator>
      <dc:date>2024-01-11T12:17:18Z</dc:date>
    </item>
    <item>
      <title>Re: Using dictionary in field calculator</title>
      <link>https://community.esri.com/t5/python-questions/using-dictionary-in-field-calculator/m-p/1368943#M69631</link>
      <description>&lt;P&gt;Are the values exactly the same between the dictionary key and the field in the table?&amp;nbsp; If there is extra text with the value in the field in the table, including any spaces, the match won't happen.&amp;nbsp; Also, does capitalization match?&amp;nbsp; It would help to provide sample data.&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jan 2024 15:25:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-dictionary-in-field-calculator/m-p/1368943#M69631</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2024-01-11T15:25:41Z</dc:date>
    </item>
    <item>
      <title>Re: Using dictionary in field calculator</title>
      <link>https://community.esri.com/t5/python-questions/using-dictionary-in-field-calculator/m-p/1369245#M69636</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/355627"&gt;@SSISC_FieldCrew&lt;/a&gt;,&amp;nbsp;to start, I would recommend updating a new field, at least until you're sure you have the logic the way you want.&amp;nbsp;&lt;/P&gt;&lt;P&gt;To follow along with what Dan showed, I tried the following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Expression:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;plant(!INV_SP_PLA!)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Code Block:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;plant_dictionary = {
    "ABUT THE": "Velvet leaf",
    "ACRO REP": "Russian knapweed",
    "AEGI CYL": "Jointed goatgrass",
    "AEGO POD": "Goutweed / bishop's weed",
    "AILA ALT": "Tree of heaven",
    "ALHA MAU": "Camel thorn",
    "ALLI PET": "Garlic mustard"
}

def plant(val):
    """Demo"""
    if val in plant_dictionary.keys():
        new_val = plant_dictionary[val]
        return new_val
    else:
        return val
    &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Given the below table, the updated values are shown in the `new` field:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="DavidWynne_0-1705010375827.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/91322iC31CB10445B154CB/image-size/medium?v=v2&amp;amp;px=400" role="button" title="DavidWynne_0-1705010375827.png" alt="DavidWynne_0-1705010375827.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I included an `else` condition to return the original value if it's not in the dictionary. Like Joshua mentioned, variations in the original field values would result in some values not being updated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jan 2024 22:10:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-dictionary-in-field-calculator/m-p/1369245#M69636</guid>
      <dc:creator>DavidWynne</dc:creator>
      <dc:date>2024-01-11T22:10:28Z</dc:date>
    </item>
    <item>
      <title>Re: Using dictionary in field calculator</title>
      <link>https://community.esri.com/t5/python-questions/using-dictionary-in-field-calculator/m-p/1369493#M69639</link>
      <description>&lt;P&gt;Yes, the values match exactly and the capitalization also.&lt;/P&gt;</description>
      <pubDate>Fri, 12 Jan 2024 11:57:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-dictionary-in-field-calculator/m-p/1369493#M69639</guid>
      <dc:creator>SSISC_FieldCrew</dc:creator>
      <dc:date>2024-01-12T11:57:14Z</dc:date>
    </item>
    <item>
      <title>Re: Using dictionary in field calculator</title>
      <link>https://community.esri.com/t5/python-questions/using-dictionary-in-field-calculator/m-p/1369494#M69640</link>
      <description>&lt;P&gt;Thanks very much&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/539"&gt;@DavidWynne&lt;/a&gt;, that did the trick.&lt;/P&gt;</description>
      <pubDate>Fri, 12 Jan 2024 11:58:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-dictionary-in-field-calculator/m-p/1369494#M69640</guid>
      <dc:creator>SSISC_FieldCrew</dc:creator>
      <dc:date>2024-01-12T11:58:57Z</dc:date>
    </item>
  </channel>
</rss>

