<?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: Calculate Field loop in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/calculate-field-loop/m-p/181383#M13955</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You forget the 2nd parameter in Calculate Field names the field to calculate values for.&amp;nbsp; You have named this "ALL"; so what your inner 'for' loop does is add to an already selected set on your feature layer (creates a new selection on the 1st iteration), then for whatever species ID was found to be populated (1), "ALL" is then calculated as 1.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The subtlety I think you're missing here is your field calculation doesn't do a sum of any kind.&amp;nbsp; It doesn't matter if performing the calculation inside or outside the loop (except it is more efficient to do it outside, after the selection is 'compiled', for lack of a better word).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;An example with a single row:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;speciesA, speciesB, speciesC, ALL&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;1, 0, 1, 1&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Of course, the value of ALL for this row is 1, correct?&amp;nbsp; (This is not a sum.)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;So you can see, if you ran the calculation within your 'for' loop, you'd be calculating the value to 1 twice, once for speciesA and once for speciesC.&amp;nbsp; (which of course is not necessary)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If you actually want the end result to be a sum, i.e., "ALL" in this case should be 2, say so and someone will likely suggest for you a more efficient way to do this, probably with an update cursor.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Enjoy,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Wayne&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 18 Jul 2013 23:04:26 GMT</pubDate>
    <dc:creator>T__WayneWhitley</dc:creator>
    <dc:date>2013-07-18T23:04:26Z</dc:date>
    <item>
      <title>Calculate Field loop</title>
      <link>https://community.esri.com/t5/python-questions/calculate-field-loop/m-p/181378#M13950</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;System Windows Vista ArcGIS 9.3&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Now I have a shape file. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Its attribute table consisted of fields of GridCell ID (GID) and &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;species ID starting with D. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;(Dxxxx (such D8729, D6745, D2765))&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The rows are GridCell ID (0-4800).&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Each cell in the attribute table has either 1 and 0.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I would like to create a new field called All.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;In the field All (Long 9, 9), the cell would should be assigned 1&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; when the cell of Dxxxx is 1. Otherwise, the cell would be 0.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The code can run the requirement above for one field. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Please kindly advise how to add the loop process for the multiple fields of Dxxxx&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you in advance.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;the code&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;##Script Name: calculate sum ##Description: calculate sum of merged range sizes of a taxonomy of migratory birds&amp;nbsp; ##Created By: Elaine Kuo ##Date: 07/18/2015&amp;nbsp;&amp;nbsp;&amp;nbsp; #Import standard library modules import arcgisscripting import os&amp;nbsp; #Create the Geoprocessor object gp = arcgisscripting.create(9.3)&amp;nbsp; #Set the workspace. gp.Workspace= "H:/temp_D/test_1"&amp;nbsp;&amp;nbsp; #Set the workspace. List all of the feature classes in the dataset outWorkspace= "H:/temp_D/test"&amp;nbsp; #Get a list of the featureclasses in the input folder fcs = gp.ListFeatureClasses()&amp;nbsp;&amp;nbsp; # Loop through every item in the list that was just generated for fc in fcs:&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; # Break out the name, no path or extension, using the describe object. &amp;nbsp;&amp;nbsp;&amp;nbsp; desc = gp.describe(fc) &amp;nbsp;&amp;nbsp;&amp;nbsp; featureName = desc.name&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; # Add a field to this shapefile, of type LONG &amp;nbsp;&amp;nbsp;&amp;nbsp; gp.AddField (fc, "All", "Long", 10,10)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; # Make temporary featureclasses &amp;nbsp;&amp;nbsp;&amp;nbsp; gp.MakeFeatureLayer(fc,"lyr") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; #&amp;nbsp;&amp;nbsp; Get a list of the fields in the featureclass &amp;nbsp;&amp;nbsp;&amp;nbsp; fields = gp.ListFields("lyr", "D*", "Long") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; # Loop through every item in the list that was just generated&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; for field in fields:&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.toolbox = "Data Management"&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Select records to be copied (C*, i.e. C7658) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; query = "\"%s\" = 1" % field.Name &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.SelectLayerByAttribute("lyr", "ADD_TO_SELECTION", query)&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # copy values in existing fields to the new field&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.CalculateField("lyr", "All", "1", "PYTHON_9.3")&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; #Validate the new feature class name for the output workspace. &amp;nbsp;&amp;nbsp;&amp;nbsp; #OutFeatureClass = outWorkspace + os.sep + gp.ValidateTableName(fc,outWorkspace)&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; # clear memory of layers &amp;nbsp;&amp;nbsp;&amp;nbsp; gp.Delete("lyr")&amp;nbsp; gp.AddMessage(gp.GetMessages()) print gp.GetMessages()&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 18 Jul 2013 08:16:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-field-loop/m-p/181378#M13950</guid>
      <dc:creator>ElaineKuo</dc:creator>
      <dc:date>2013-07-18T08:16:27Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Field loop</title>
      <link>https://community.esri.com/t5/python-questions/calculate-field-loop/m-p/181379#M13951</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I think I see what you are doing, although I believe I'd do it with an updatecursor.&amp;nbsp; Am I correct that you ask for the remaining records for the 'ALL' field be coded 0 after you have coded the others 1?&amp;nbsp; If so, to carry on with what you already have, see the below.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I noticed you're looping through a list of fields to form your sel query, so your fields must be varying depending on what fc or table you feed in.&amp;nbsp; Then you're performing a 'select by attributes' based on the formed query using 'add to selection' param -- because of this, you don't need to 'recalculate' the previous selections in successive loop iterations.&amp;nbsp; What I mean is you don't need to indent this line within your 'for' loop:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;gp.CalculateField("lyr", "All", "1", "PYTHON_9.3")&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Run this outside your loop, then follow that with another 'select by attributes' - I believe it has a 'switch selection' parameter.&amp;nbsp; Then run your field calculation again, as in:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;gp.SelectLayerByAttribute_management("lyr", "SWITCH_SELECTION")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;gp.CalculateField("lyr", "All", "0", "PYTHON_9.3")&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This is untested, so hope that works for you...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Enjoy,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Wayne&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;PS - See my line of code that included the toolbox alias, "_management".&amp;nbsp; If you call your tools in that manner then you don't need lines such as the one you included:&amp;nbsp; gp.toolbox = "Data Management"&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 18 Jul 2013 11:17:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-field-loop/m-p/181379#M13951</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2013-07-18T11:17:46Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Field loop</title>
      <link>https://community.esri.com/t5/python-questions/calculate-field-loop/m-p/181380#M13952</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello Wayne, &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I tested my code and it worked well.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Actually I want to have a summary field of &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;the presence (1) of species ID (column) in the GridCell ID (row). &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In other words, the GridCell of the field "All" should be assigned as 1, &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;as long as&amp;nbsp; the GridCells of D8729, D6745, D2765 is 1. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks for reminding and help, Wayne.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
##Script Name: calculate sum
##Description: calculate sum of merged range sizes of a taxonomy of migratory birds 
##Created By: Elaine Kuo
##Date: 07/18/2015
 

#Import standard library modules
import arcgisscripting
import os

#Create the Geoprocessor object
gp = arcgisscripting.create(9.3)

#Set the workspace.
gp.Workspace= "H:/temp_D/test_1" 

#Set the workspace. List all of the feature classes in the dataset
outWorkspace= "H:/temp_D/test"

#Get a list of the featureclasses in the input folder
fcs = gp.ListFeatureClasses() 

# Loop through every item in the list that was just generated
for fc in fcs:

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Break out the name, no path or extension, using the describe object.
&amp;nbsp;&amp;nbsp;&amp;nbsp; desc = gp.describe(fc)
&amp;nbsp;&amp;nbsp;&amp;nbsp; featureName = desc.name

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Add a field to this shapefile, of type LONG
&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.AddField (fc, "All", "Long", 10,10)&amp;nbsp;&amp;nbsp; 

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Make temporary featureclasses
&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.MakeFeatureLayer(fc,"lyr")
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; #&amp;nbsp;&amp;nbsp; Get a list of the fields in the featureclass
&amp;nbsp;&amp;nbsp;&amp;nbsp; fields = gp.ListFields("lyr", "D*", "Long")
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Loop through every item in the list that was just generated 
&amp;nbsp;&amp;nbsp;&amp;nbsp; for field in fields:

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.toolbox = "Data Management"

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Select records to be copied (C*, i.e. C7658)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; query = "\"%s\" = 1" % field.Name
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.SelectLayerByAttribute("lyr", "ADD_TO_SELECTION", query)

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # copy values in existing fields to the new field 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.CalculateField("lyr", "All", "1", "PYTHON_9.3")&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 

&amp;nbsp;&amp;nbsp;&amp;nbsp; #Validate the new feature class name for the output workspace.
&amp;nbsp;&amp;nbsp;&amp;nbsp; #OutFeatureClass = outWorkspace + os.sep + gp.ValidateTableName(fc,outWorkspace)

&amp;nbsp;&amp;nbsp;&amp;nbsp; # clear memory of layers
&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.Delete("lyr")

gp.AddMessage(gp.GetMessages())
print gp.GetMessages()
&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;E.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 09:16:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-field-loop/m-p/181380#M13952</guid>
      <dc:creator>ElaineKuo</dc:creator>
      <dc:date>2021-12-11T09:16:14Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Field loop</title>
      <link>https://community.esri.com/t5/python-questions/calculate-field-loop/m-p/181381#M13953</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Soooo, your code does as you wish it to do now?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am not sure if you have realized yet that you frequently mark your own post as 'the answer'.&amp;nbsp; Basically, you can do either or both of 2 actions:&amp;nbsp; award points to various posts and/or award the &lt;/SPAN&gt;&lt;SPAN style="text-decoration:underline;"&gt;best answer&lt;/SPAN&gt;&lt;SPAN&gt; (if there is one) to a single post.&amp;nbsp; It is at least unclear to me anything was resolved... &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Also, although it isn't always possible to do so, it is considered 'good etiquette' or at least good practice to post your final code or explain if you resolved your problem in a different way.&amp;nbsp; Pretend you have colleagues following up behind you with a similar question researching the thread you started here - did you leave them in the dark?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Keep trying, and...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Enjoy,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Wayne&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 18 Jul 2013 22:23:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-field-loop/m-p/181381#M13953</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2013-07-18T22:23:07Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Field loop</title>
      <link>https://community.esri.com/t5/python-questions/calculate-field-loop/m-p/181382#M13954</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;gp.CalculateField("lyr", "All", "1", "PYTHON_9.3")&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Run this outside your loop, then follow that with another 'select by attributes' - I believe it has a 'switch selection' parameter. Then run your field calculation again, as in:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;=&amp;gt; I think the "gp.CalculateField" is in need in the loop, &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; because the information of every field of Species ID (Dxxxx) needs to be updated in the field "All." &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; The field "All" is a summary field of the multiple Species ID (Dxxxx).&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 18 Jul 2013 22:43:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-field-loop/m-p/181382#M13954</guid>
      <dc:creator>ElaineKuo</dc:creator>
      <dc:date>2013-07-18T22:43:22Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Field loop</title>
      <link>https://community.esri.com/t5/python-questions/calculate-field-loop/m-p/181383#M13955</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You forget the 2nd parameter in Calculate Field names the field to calculate values for.&amp;nbsp; You have named this "ALL"; so what your inner 'for' loop does is add to an already selected set on your feature layer (creates a new selection on the 1st iteration), then for whatever species ID was found to be populated (1), "ALL" is then calculated as 1.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The subtlety I think you're missing here is your field calculation doesn't do a sum of any kind.&amp;nbsp; It doesn't matter if performing the calculation inside or outside the loop (except it is more efficient to do it outside, after the selection is 'compiled', for lack of a better word).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;An example with a single row:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;speciesA, speciesB, speciesC, ALL&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;1, 0, 1, 1&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Of course, the value of ALL for this row is 1, correct?&amp;nbsp; (This is not a sum.)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;So you can see, if you ran the calculation within your 'for' loop, you'd be calculating the value to 1 twice, once for speciesA and once for speciesC.&amp;nbsp; (which of course is not necessary)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If you actually want the end result to be a sum, i.e., "ALL" in this case should be 2, say so and someone will likely suggest for you a more efficient way to do this, probably with an update cursor.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Enjoy,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Wayne&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 18 Jul 2013 23:04:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-field-loop/m-p/181383#M13955</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2013-07-18T23:04:26Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Field loop</title>
      <link>https://community.esri.com/t5/python-questions/calculate-field-loop/m-p/181384#M13956</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;You forget the 2nd parameter in Calculate Field names the field to calculate values for.&amp;nbsp; You have named this "ALL"; so what your inner 'for' loop does is add to an already selected set on your feature layer (creates a new selection on the 1st iteration), then for whatever species ID was found to be populated (1), "ALL" is then calculated as 1.&lt;BR /&gt;&lt;BR /&gt;The subtlety I think you're missing here is your field calculation doesn't do a sum of any kind.&amp;nbsp; It doesn't matter if performing the calculation inside or outside the loop (except it is more efficient to do it outside, after the selection is 'compiled', for lack of a better word).&lt;BR /&gt;&lt;BR /&gt;An example with a single row:&lt;BR /&gt;&lt;BR /&gt;speciesA, speciesB, speciesC, ALL&lt;BR /&gt;1, 0, 1, 1&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Of course, the value of ALL for this row is 1, correct?&amp;nbsp; (This is not a sum.)&lt;BR /&gt;So you can see, if you ran the calculation within your 'for' loop, you'd be calculating the value to 1 twice, once for speciesA and once for speciesC.&amp;nbsp; (which of course is not necessary)&lt;BR /&gt;&lt;BR /&gt;Wayne&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks for reminding.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;1. Your explanation on putting the gp.CalculateField outside the loop is very clear.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; I will modify my code according to your advice.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;2. sorry for misusing the word "sum."&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; Actually I just want to record whether the GridCell is 1, not the total number of "1" summed up.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Elaine&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 19 Jul 2013 03:12:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-field-loop/m-p/181384#M13956</guid>
      <dc:creator>ElaineKuo</dc:creator>
      <dc:date>2013-07-19T03:12:08Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Field loop</title>
      <link>https://community.esri.com/t5/python-questions/calculate-field-loop/m-p/181385#M13957</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;For clarity, I'll post below your code with modifications we discussed - I didn't test it, that's up to you.&amp;nbsp; Note that this is certainly not the only way to perform your desired task.&amp;nbsp; One more thing - check your Date in the script file header -- I am assuming you intend this to be a 'created' date and to have this completed this year, not 2015?&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;##Script Name: calculate sum ##Description: calculate sum of merged range sizes of a taxonomy of migratory birds&amp;nbsp; ##Created By: Elaine Kuo ##Date: 07/18/2015&amp;nbsp;&amp;nbsp;&amp;nbsp; #Import standard library modules import arcgisscripting import os&amp;nbsp; #Create the Geoprocessor object gp = arcgisscripting.create(9.3)&amp;nbsp; #Set the workspace. gp.Workspace= "H:/temp_D/test_1"&amp;nbsp;&amp;nbsp; #Set the workspace. List all of the feature classes in the dataset outWorkspace= "H:/temp_D/test"&amp;nbsp; #Get a list of the featureclasses in the input folder fcs = gp.ListFeatureClasses()&amp;nbsp;&amp;nbsp; # Loop through every item in the list that was just generated for fc in fcs:&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; # Break out the name, no path or extension, using the describe object. &amp;nbsp;&amp;nbsp;&amp;nbsp; desc = gp.describe(fc) &amp;nbsp;&amp;nbsp;&amp;nbsp; featureName = desc.name&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; # Add a field to this shapefile, of type LONG &amp;nbsp;&amp;nbsp;&amp;nbsp; gp.AddField (fc, "All", "Long", 10,10)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; # Make temporary featureclasses &amp;nbsp;&amp;nbsp;&amp;nbsp; gp.MakeFeatureLayer(fc,"lyr") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; #&amp;nbsp;&amp;nbsp; Get a list of the fields in the featureclass &amp;nbsp;&amp;nbsp;&amp;nbsp; fields = gp.ListFields("lyr", "D*", "Long") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; # Loop through every item in the list that was just generated&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; for field in fields: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Select records to use in a field calculation (C*, i.e. C7658) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; query = "\"%s\" = 1" % field.Name &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.SelectLayerByAttribute_management("lyr", "ADD_TO_SELECTION", query)&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; # code the 'All' field of the net selected set with a value of 1 &amp;nbsp;&amp;nbsp;&amp;nbsp; gp.CalculateField_management("lyr", "All", "1", "PYTHON_9.3") &amp;nbsp;&amp;nbsp;&amp;nbsp; gp.SelectLayerByAttribute_management("lyr", "SWITCH_SELECTION") &amp;nbsp;&amp;nbsp;&amp;nbsp; gp.CalculateField_management("lyr", "All", "0", "PYTHON_9.3")&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; #Validate the new feature class name for the output workspace. &amp;nbsp;&amp;nbsp;&amp;nbsp; #OutFeatureClass = outWorkspace + os.sep + gp.ValidateTableName(fc,outWorkspace)&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; # clear memory of layers &amp;nbsp;&amp;nbsp;&amp;nbsp; gp.Delete("lyr")&amp;nbsp; gp.AddMessage(gp.GetMessages()) print gp.GetMessages()&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Careful with indention, easy to make an error there!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Enjoy,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Wayne&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 19 Jul 2013 16:35:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-field-loop/m-p/181385#M13957</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2013-07-19T16:35:36Z</dc:date>
    </item>
  </channel>
</rss>

