<?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 an external Dictionary that multiple scripts in model builder can reference for a reclass in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/using-an-external-dictionary-that-multiple-scripts/m-p/1130600#M63399</link>
    <description>&lt;P&gt;You can use the dictionaries get() method.&amp;nbsp; It returns either the matching key's value or the default ShortNameField if the LongNameField is not matched to a key:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;nameDict = {"LONG NAME 1": "LN 1", "LONG NAME 2": "LN 2", "LONG NAME 4": "LN 4"}

val = nameDict.get(LongNameField, ShortNameField)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;edit to add: You can read a csv to a dictionary fairly easy, (lots of examples out there) setting a column as key and the other column as its value for each row.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;with open('your.csv', mode='r') as infile:
    reader = csv.reader(infile)
    nameDict = dict((rows[0],rows[1]) for rows in reader)&lt;/LI-CODE&gt;</description>
    <pubDate>Tue, 04 Jan 2022 22:16:21 GMT</pubDate>
    <dc:creator>Anonymous User</dc:creator>
    <dc:date>2022-01-04T22:16:21Z</dc:date>
    <item>
      <title>Using an external Dictionary that multiple scripts in model builder can reference for a reclass</title>
      <link>https://community.esri.com/t5/python-questions/using-an-external-dictionary-that-multiple-scripts/m-p/1130494#M63397</link>
      <description>&lt;P&gt;Hello All, first time asking a question, thanks for your help in advance&lt;/P&gt;&lt;P&gt;I am using one field in order to fill another field in model builder.&amp;nbsp; Now most of this rename just uses the first word of the name to fill the second and that is done in a previous step.&amp;nbsp; Where it uses this script is when that method doesn't work and it needs a particular name for the starting name.&amp;nbsp; The way that I do this now is with the script below but since I do this same process multiple times on different datasets I have to update the list in multiple places in the model.&amp;nbsp; What I would like to do is have a single dictionary with all the name pairs that all the separate scripts can reference, something like a CSV as a dictionary with each name pair.&amp;nbsp; Is this possible?&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;FEILD NAME&lt;BR /&gt;ShortNameField&lt;/P&gt;&lt;P&gt;EXPRESSION&lt;BR /&gt;reclass( !LongNameField!, !ShortNameField! )&lt;/P&gt;&lt;P&gt;CODE BLOCK&lt;BR /&gt;def reclass( LongNameField,ShortNameField &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;&lt;P&gt;if (LongNameField == "LONG NAME 1"):&lt;BR /&gt;return "LN 1"&lt;BR /&gt;elif (LongNameField == "LONG NAME 2"):&lt;BR /&gt;return "LN 2"&lt;BR /&gt;elif (LongNameField == "LONG NAME 3"):&lt;BR /&gt;return "LN 3"&lt;BR /&gt;elif (LongNameField == "LONG NAME 4"):&lt;BR /&gt;return "LN 4"&lt;BR /&gt;[etc for hundreds of names]&lt;BR /&gt;else:&lt;BR /&gt;return ShortNameField&lt;/P&gt;</description>
      <pubDate>Tue, 04 Jan 2022 19:02:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-an-external-dictionary-that-multiple-scripts/m-p/1130494#M63397</guid>
      <dc:creator>PatrickAllen42</dc:creator>
      <dc:date>2022-01-04T19:02:46Z</dc:date>
    </item>
    <item>
      <title>Re: Using an external Dictionary that multiple scripts in model builder can reference for a reclass</title>
      <link>https://community.esri.com/t5/python-questions/using-an-external-dictionary-that-multiple-scripts/m-p/1130600#M63399</link>
      <description>&lt;P&gt;You can use the dictionaries get() method.&amp;nbsp; It returns either the matching key's value or the default ShortNameField if the LongNameField is not matched to a key:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;nameDict = {"LONG NAME 1": "LN 1", "LONG NAME 2": "LN 2", "LONG NAME 4": "LN 4"}

val = nameDict.get(LongNameField, ShortNameField)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;edit to add: You can read a csv to a dictionary fairly easy, (lots of examples out there) setting a column as key and the other column as its value for each row.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;with open('your.csv', mode='r') as infile:
    reader = csv.reader(infile)
    nameDict = dict((rows[0],rows[1]) for rows in reader)&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 04 Jan 2022 22:16:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-an-external-dictionary-that-multiple-scripts/m-p/1130600#M63399</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2022-01-04T22:16:21Z</dc:date>
    </item>
  </channel>
</rss>

