<?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: For loop through field values in Calculate Field tool in ArcGIS Pro Questions</title>
    <link>https://community.esri.com/t5/arcgis-pro-questions/for-loop-through-field-values-in-calculate-field/m-p/1030657#M37907</link>
    <description>&lt;P&gt;As a field calculation, all it is going to do is sum each value in that row.&lt;/P&gt;&lt;P&gt;It will not cycle through all the rows and produce some big cumulative sum.&lt;/P&gt;&lt;P&gt;That is not how field calculations work.&amp;nbsp; The work a row at a time.&lt;/P&gt;</description>
    <pubDate>Thu, 25 Feb 2021 20:07:58 GMT</pubDate>
    <dc:creator>DanPatterson</dc:creator>
    <dc:date>2021-02-25T20:07:58Z</dc:date>
    <item>
      <title>For loop through field values in Calculate Field tool</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/for-loop-through-field-values-in-calculate-field/m-p/1030627#M37902</link>
      <description>&lt;P&gt;Pulling my hair out here...I'm trying to rebuild a for loop in a field calculator tool in modelbuilder. No matter what I try, the for loop only returns the last value of the iteration. This should be so simple, but I've been stuck for hours! Tried all sorts of reconfigurations.&lt;/P&gt;&lt;P&gt;I have several fields with H,M,L or N values. Each of those should be assigned a score, which is then summed and returned for the new field. No matter what I do, the returned value is caculated ONLY based on the last field in the list as if I had the sum = 0 line inside the For loop...but I don't.&lt;/P&gt;&lt;P&gt;Any ideas?&lt;/P&gt;&lt;P&gt;CalcFVScore([!Field1!,!Field2!,!Field3!,!Field4!])&lt;/P&gt;&lt;P&gt;Code Block:&lt;/P&gt;&lt;P&gt;def CalcFVScore(fields):&lt;BR /&gt;&amp;nbsp; &amp;nbsp; sum = 0&lt;BR /&gt;&amp;nbsp; &amp;nbsp; for f in fields:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if f == "H":&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; score = 5&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; elif f == "M":&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; score = 3&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; elif f == "L":&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; score = 1&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; else:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; score = 0&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; sum = sum + score&lt;BR /&gt;&amp;nbsp; &amp;nbsp; return sum&lt;/P&gt;</description>
      <pubDate>Thu, 25 Feb 2021 19:37:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/for-loop-through-field-values-in-calculate-field/m-p/1030627#M37902</guid>
      <dc:creator>AaronWorthley1</dc:creator>
      <dc:date>2021-02-25T19:37:46Z</dc:date>
    </item>
    <item>
      <title>Re: For loop through field values in Calculate Field tool</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/for-loop-through-field-values-in-calculate-field/m-p/1030649#M37903</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;flds = ['H', 'L', 'M', 'N']

def CalcFVScore(flds):
    sum = 0
    for f in flds:
        if f == "H":
            sum += 5
        elif f == "M":
            sum += 3
        elif f == "L":
            sum += 1
        else:
            sum += 0
    return sum
    

CalcFVScore(flds)
Out[3]: 9&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Feb 2021 20:05:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/for-loop-through-field-values-in-calculate-field/m-p/1030649#M37903</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-02-25T20:05:42Z</dc:date>
    </item>
    <item>
      <title>Re: For loop through field values in Calculate Field tool</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/for-loop-through-field-values-in-calculate-field/m-p/1030650#M37904</link>
      <description>&lt;P&gt;I don't have any trouble running your code when I test it. However, you may want to change your variable name, as &lt;STRONG&gt;sum&lt;/STRONG&gt; is a built-in function, and perhaps it's throwing something off. I replaced "sum" → "s" and it ran just fine.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Feb 2021 19:57:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/for-loop-through-field-values-in-calculate-field/m-p/1030650#M37904</guid>
      <dc:creator>jcarlson</dc:creator>
      <dc:date>2021-02-25T19:57:43Z</dc:date>
    </item>
    <item>
      <title>Re: For loop through field values in Calculate Field tool</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/for-loop-through-field-values-in-calculate-field/m-p/1030653#M37905</link>
      <description>&lt;P&gt;It runs just fine for me as well, the problem is the solution is wrong. It only returns the result of the last field in the list. If I change the list, the result changes...it's not summing the cumulative values from the for iterations, only the value from the last iteration.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Feb 2021 20:04:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/for-loop-through-field-values-in-calculate-field/m-p/1030653#M37905</guid>
      <dc:creator>AaronWorthley1</dc:creator>
      <dc:date>2021-02-25T20:04:57Z</dc:date>
    </item>
    <item>
      <title>Re: For loop through field values in Calculate Field tool</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/for-loop-through-field-values-in-calculate-field/m-p/1030654#M37906</link>
      <description>&lt;P&gt;Tried this, does the same thing. Only returns the calculated value of the last item (field value) in the iteration list.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Feb 2021 20:05:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/for-loop-through-field-values-in-calculate-field/m-p/1030654#M37906</guid>
      <dc:creator>AaronWorthley1</dc:creator>
      <dc:date>2021-02-25T20:05:54Z</dc:date>
    </item>
    <item>
      <title>Re: For loop through field values in Calculate Field tool</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/for-loop-through-field-values-in-calculate-field/m-p/1030657#M37907</link>
      <description>&lt;P&gt;As a field calculation, all it is going to do is sum each value in that row.&lt;/P&gt;&lt;P&gt;It will not cycle through all the rows and produce some big cumulative sum.&lt;/P&gt;&lt;P&gt;That is not how field calculations work.&amp;nbsp; The work a row at a time.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Feb 2021 20:07:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/for-loop-through-field-values-in-calculate-field/m-p/1030657#M37907</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-02-25T20:07:58Z</dc:date>
    </item>
    <item>
      <title>Re: For loop through field values in Calculate Field tool</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/for-loop-through-field-values-in-calculate-field/m-p/1030663#M37908</link>
      <description>&lt;P&gt;This is what gets returned. If I reduce the number of input fields, whatever is the last one is what gives up the value.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="vFpVxyfWMr.png" style="width: 675px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/6958i5808707C5477C37A/image-size/large?v=v2&amp;amp;px=999" role="button" title="vFpVxyfWMr.png" alt="vFpVxyfWMr.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Feb 2021 20:12:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/for-loop-through-field-values-in-calculate-field/m-p/1030663#M37908</guid>
      <dc:creator>AaronWorthley1</dc:creator>
      <dc:date>2021-02-25T20:12:57Z</dc:date>
    </item>
    <item>
      <title>Re: For loop through field values in Calculate Field tool</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/for-loop-through-field-values-in-calculate-field/m-p/1030666#M37910</link>
      <description>&lt;P&gt;confusion... you only show 4 fields in your example.&amp;nbsp; Hopefully those are the actual field names.&lt;/P&gt;&lt;P&gt;You need to provide the "Field Names".... Your example suggests that those are the field names, not the values in those fields.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you want to sum across the row for the number of occurences of H, M, L, N etc.... then you need to field the field names into the fields list&lt;/P&gt;</description>
      <pubDate>Thu, 25 Feb 2021 20:24:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/for-loop-through-field-values-in-calculate-field/m-p/1030666#M37910</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-02-25T20:24:51Z</dc:date>
    </item>
    <item>
      <title>Re: For loop through field values in Calculate Field tool</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/for-loop-through-field-values-in-calculate-field/m-p/1030671#M37911</link>
      <description>&lt;P&gt;I just substituted my actual field names with !Field1!,!Field2! for the purposes of this example.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Feb 2021 20:29:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/for-loop-through-field-values-in-calculate-field/m-p/1030671#M37911</guid>
      <dc:creator>AaronWorthley1</dc:creator>
      <dc:date>2021-02-25T20:29:21Z</dc:date>
    </item>
    <item>
      <title>Re: For loop through field values in Calculate Field tool</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/for-loop-through-field-values-in-calculate-field/m-p/1030672#M37912</link>
      <description>&lt;P&gt;I think it's solved...first I found a domain issue in the geodatabase- the field had both a domain assigned as well as a subtype with domain. Running again I had the same issue, but then I deleted all the fields from the expression, and re-entered them one by one, running each time to see if it worked. After adding them all back it seems to be returning correct results.&lt;/P&gt;&lt;P&gt;I cannot explain it, thanks for taking a look. I did use Dan's version to simplify the code which is a good improvement.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Feb 2021 20:33:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/for-loop-through-field-values-in-calculate-field/m-p/1030672#M37912</guid>
      <dc:creator>AaronWorthley1</dc:creator>
      <dc:date>2021-02-25T20:33:13Z</dc:date>
    </item>
    <item>
      <title>Re: For loop through field values in Calculate Field tool</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/for-loop-through-field-values-in-calculate-field/m-p/1030673#M37913</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def CalcFVScore(flds):
    sum = 0
    for f in flds:
        if "H" in f:
            sum += 5
        elif "M" in f:
            sum += 3
        elif "L" in f:
            sum += 1
        else:
            sum += 0
    return sum&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;in case there are stray things in the fields.&lt;/P&gt;&lt;P&gt;Try it with 2 fields first to see if it is passing the list of field names properly&lt;/P&gt;</description>
      <pubDate>Thu, 25 Feb 2021 20:37:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/for-loop-through-field-values-in-calculate-field/m-p/1030673#M37913</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-02-25T20:37:23Z</dc:date>
    </item>
  </channel>
</rss>

