<?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: Conditional Calculate Field in Python in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/conditional-calculate-field-in-python/m-p/337476#M26466</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Wow, that's some pretty cool stuff!&amp;nbsp; Thanks for sharing.&amp;nbsp; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 16 Jun 2015 20:57:43 GMT</pubDate>
    <dc:creator>CoyPotts1</dc:creator>
    <dc:date>2015-06-16T20:57:43Z</dc:date>
    <item>
      <title>Conditional Calculate Field in Python</title>
      <link>https://community.esri.com/t5/python-questions/conditional-calculate-field-in-python/m-p/337473#M26463</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have a script that works, but I replicate it in order to conditionally update 5 different fields.&amp;nbsp; I would like to bolster the script to conditionally calculate all 5 fields into one if I can.&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# Calculate Region

# Set local variables
inTable = nodeFeatures
inField = "Region1"
expression = "Reclass (!NodeID1!, !Region1!)"
codeBlock = """def Reclass (NodeID1, Region1):
&amp;nbsp;&amp;nbsp;&amp;nbsp; if NodeID1 != None:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return region&amp;nbsp;&amp;nbsp;&amp;nbsp; #variable is declared earlier in the script
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return None"""

# Execute CalculateField
arcpy.CalculateField_management(inTable, inField, expression, "PYTHON_9.3", codeBlock)

&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The script above works just fine, but as I mentioned, I end up duplicating it to calculate 4 other fields.&amp;nbsp; In this example we'll call all 5 fields Region1, Region2, etc...&amp;nbsp; They would all have an accompanying NodeID1, NodeID2, etc...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Is it possible to expand upon this script in order to reduce the 5 separate scripts to calculate them all in one, or is it just the way it has to be in order to calculate a value of completely separate fields?&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Thanks in advance!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:59:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/conditional-calculate-field-in-python/m-p/337473#M26463</guid>
      <dc:creator>CoyPotts1</dc:creator>
      <dc:date>2021-12-11T15:59:04Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional Calculate Field in Python</title>
      <link>https://community.esri.com/t5/python-questions/conditional-calculate-field-in-python/m-p/337474#M26464</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;See Richard Fairhurst's blog &lt;A href="https://community.esri.com/migration-blogpost/1885"&gt;Turbo Charging Data Manipulation with Python Cursors and Dictionaries&lt;/A&gt; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 16 Jun 2015 20:39:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/conditional-calculate-field-in-python/m-p/337474#M26464</guid>
      <dc:creator>WesMiller</dc:creator>
      <dc:date>2015-06-16T20:39:52Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional Calculate Field in Python</title>
      <link>https://community.esri.com/t5/python-questions/conditional-calculate-field-in-python/m-p/337475#M26465</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;&lt;SPAN class="comment" style="font-weight: inherit; font-style: inherit; color: #008200; font-size: 9pt !important; background-color: inherit;"&gt;You can place everything in a loop, counting from 1 - 5. Then, it's a matter of substituting the value into your expressions. This is untested, and I almost promise I got something wrong, but it should be a start:&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;&lt;SPAN class="comment" style="font-weight: inherit; font-style: inherit; color: #008200; font-size: 9pt !important; background-color: inherit;"&gt;# Calculate Region&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;

&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;for i in range(1,6):&amp;nbsp; &lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;&lt;SPAN class="comment" style="font-weight: inherit; font-style: inherit; color: #008200; font-size: 9pt !important; background-color: inherit;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Set local variables&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; inTable = nodeFeatures&amp;nbsp; &lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; inField = &lt;SPAN class="string" style="font-weight: inherit; font-style: inherit; color: blue; font-size: 9pt !important; background-color: inherit;"&gt;"Region" + i&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; expression = &lt;SPAN class="string" style="font-weight: inherit; font-style: inherit; color: blue; font-size: 9pt !important; background-color: inherit;"&gt;"Reclass (!NodeID" + i +"!, !Region" + i + "!)"&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; codeBlock = &lt;SPAN class="comment" style="font-weight: inherit; font-style: inherit; color: #008200; font-size: 9pt !important; background-color: inherit;"&gt;"""def Reclass (NodeID" + i + ", Region" + i + "):&lt;/SPAN&gt; &lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;&lt;SPAN class="comment" style="font-weight: inherit; font-style: inherit; color: #008200; font-size: 9pt !important; background-color: inherit;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if NodeID" + i + "!= None:&lt;/SPAN&gt; &lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;&lt;SPAN class="comment" style="font-weight: inherit; font-style: inherit; color: #008200; font-size: 9pt !important; background-color: inherit;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return region&amp;nbsp;&amp;nbsp;&amp;nbsp; #variable is declared earlier in the script&lt;/SPAN&gt; &lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;&lt;SPAN class="comment" style="font-weight: inherit; font-style: inherit; color: #008200; font-size: 9pt !important; background-color: inherit;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&lt;/SPAN&gt; &lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;&lt;SPAN class="comment" style="font-weight: inherit; font-style: inherit; color: #008200; font-size: 9pt !important; background-color: inherit;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return None"""&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;&amp;nbsp; &lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;&lt;SPAN class="comment" style="font-weight: inherit; font-style: inherit; color: #008200; font-size: 9pt !important; background-color: inherit;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Execute CalculateField&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(inTable, inField, expression, &lt;SPAN class="string" style="font-weight: inherit; font-style: inherit; color: blue; font-size: 9pt !important; background-color: inherit;"&gt;"PYTHON_9.3"&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt;, codeBlock) &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:59:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/conditional-calculate-field-in-python/m-p/337475#M26465</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2021-12-11T15:59:06Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional Calculate Field in Python</title>
      <link>https://community.esri.com/t5/python-questions/conditional-calculate-field-in-python/m-p/337476#M26466</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Wow, that's some pretty cool stuff!&amp;nbsp; Thanks for sharing.&amp;nbsp; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 16 Jun 2015 20:57:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/conditional-calculate-field-in-python/m-p/337476#M26466</guid>
      <dc:creator>CoyPotts1</dc:creator>
      <dc:date>2015-06-16T20:57:43Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional Calculate Field in Python</title>
      <link>https://community.esri.com/t5/python-questions/conditional-calculate-field-in-python/m-p/337477#M26467</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;This looks like it'll do just what I need.&amp;nbsp; I'll give it a try a little later and respond with my results.&amp;nbsp; Thanks for responding.&amp;nbsp; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 16 Jun 2015 20:58:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/conditional-calculate-field-in-python/m-p/337477#M26467</guid>
      <dc:creator>CoyPotts1</dc:creator>
      <dc:date>2015-06-16T20:58:13Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional Calculate Field in Python</title>
      <link>https://community.esri.com/t5/python-questions/conditional-calculate-field-in-python/m-p/337478#M26468</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Actually, I made an oversight when I pasted my code.&amp;nbsp; I omitted some of the verbiage to not include customer names and whatnot, so instead of each NodeID field literally having a number after it, each number actually represents a customer name.&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;BR /&gt;But I will still use your sample code as reference for a FOR statement!&amp;nbsp; &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/happy.png" /&gt;. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Can you think of a way to do it without the FOR iteration? &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 18 Jun 2015 19:25:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/conditional-calculate-field-in-python/m-p/337478#M26468</guid>
      <dc:creator>CoyPotts1</dc:creator>
      <dc:date>2015-06-18T19:25:01Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional Calculate Field in Python</title>
      <link>https://community.esri.com/t5/python-questions/conditional-calculate-field-in-python/m-p/337479#M26469</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Do you mean you want to loop through names, rather than numbers? If so, you can iterate through a list:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; list = ['Bob','Dave','Henry']
... for name in list:
...&amp;nbsp;&amp;nbsp;&amp;nbsp; print name
Bob
Dave
Henry&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;...or a dictionary:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; dict = {1:'a', 2:'b', 3:'c'}
... for item in dict:
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print item
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print dict[item]
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
1
a
2
b
3
c&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;...or a tuple:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; tuple = (1, 2, 3)
... for item in tuple:
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print item
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
1
2
3&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You can loop through unexpected things, too:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; string = "Hello"
... for letter in string:
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print letter
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
H
e
l
l
o&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:59:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/conditional-calculate-field-in-python/m-p/337479#M26469</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2021-12-11T15:59:09Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional Calculate Field in Python</title>
      <link>https://community.esri.com/t5/python-questions/conditional-calculate-field-in-python/m-p/337480#M26470</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;This may be getting me in the right direction.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The feature class that I am using has duplicate fields for each customer, so say each customer has the same 20 fields, and the only difference between them is their customer identification. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I want to loop through and say something like the following:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;if Customer1NodeID != None:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return regionvariable
if Customer2NodeID != None:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return regionvariable
if Customer3NodeID != None:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return regionvariable
else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return None&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm just not sure how to nest multiple fields to be updated into one loop.&amp;nbsp; Essentially I have 5 customers that I want to add data to their respective fields, but only if their NodeID field has data in it, otherwise leave it blank. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:59:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/conditional-calculate-field-in-python/m-p/337480#M26470</guid>
      <dc:creator>CoyPotts1</dc:creator>
      <dc:date>2021-12-11T15:59:12Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional Calculate Field in Python</title>
      <link>https://community.esri.com/t5/python-questions/conditional-calculate-field-in-python/m-p/337481#M26471</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;as an example...there are better data structures ( eg dict)&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt;
&amp;gt;&amp;gt;&amp;gt; in_group = ['a','b','c','d','e']
&amp;gt;&amp;gt;&amp;gt; has_stuff = ['a','d','e']
&amp;gt;&amp;gt;&amp;gt; for i in in_group:
...&amp;nbsp; if i in has_stuff:
...&amp;nbsp; print("{} from the in_group has stuff".format(i))
...&amp;nbsp; else:
...&amp;nbsp; print("{}'s got nothing...".format(i))
... 
a from the in_group has stuff
b's got nothing...
c's got nothing...
d from the in_group has stuff
e from the in_group has stuff
&amp;gt;&amp;gt;&amp;gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:59:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/conditional-calculate-field-in-python/m-p/337481#M26471</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2021-12-11T15:59:14Z</dc:date>
    </item>
  </channel>
</rss>

