<?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: Function in loop in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/function-in-loop/m-p/1048412#M60831</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&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;&lt;P&gt;&amp;nbsp;&lt;/P&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>Sat, 17 Apr 2021 09:22:59 GMT</pubDate>
    <dc:creator>Mick</dc:creator>
    <dc:date>2021-04-17T09:22:59Z</dc:date>
    <item>
      <title>Function in loop</title>
      <link>https://community.esri.com/t5/python-questions/function-in-loop/m-p/1048406#M60828</link>
      <description>&lt;P&gt;Could you please correct my function of the main script in order for the new call to list the next value in the list?&lt;/P&gt;&lt;P&gt;I am definitely sure that the problem in my function because it lists only the first value in States_name list.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 18 Apr 2021 09:04:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/function-in-loop/m-p/1048406#M60828</guid>
      <dc:creator>Mick</dc:creator>
      <dc:date>2021-04-18T09:04:25Z</dc:date>
    </item>
    <item>
      <title>Re: Function in loop</title>
      <link>https://community.esri.com/t5/python-questions/function-in-loop/m-p/1048407#M60829</link>
      <description>&lt;P&gt;It's a bit hard to say much as you don't include enough detail (i.e. where you get the "States_names" lists from, you didn't include your function "def", or show where you get "pathway" from or what you do with it in the function).&amp;nbsp; However, if I assume the "States_names" list is the same in the function as it is in the main script... then it will only ever return the first value because you return in the first iteration of the loop.&lt;/P&gt;&lt;P&gt;Try something like:&lt;/P&gt;&lt;P&gt;makeStateLayer.py&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def makeStateLayer(state_path, state_name):
    query = "StateField = '{0}'".format(state_name)
    #Create a feature layer containing one state
    statesLayer = arcpy.MakeFeatureLayer_management(state_path, "States_Layer", query)[0]
    return statesLayer&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;main_script.py&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from makeStateLayer import makeStateLayer

for name in States_names:
    stateLayer = makeStateLayer(pathway, name)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 17 Apr 2021 06:54:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/function-in-loop/m-p/1048407#M60829</guid>
      <dc:creator>Luke_Pinner</dc:creator>
      <dc:date>2021-04-17T06:54:45Z</dc:date>
    </item>
    <item>
      <title>Re: Function in loop</title>
      <link>https://community.esri.com/t5/python-questions/function-in-loop/m-p/1048412#M60831</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&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;&lt;P&gt;&amp;nbsp;&lt;/P&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>Sat, 17 Apr 2021 09:22:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/function-in-loop/m-p/1048412#M60831</guid>
      <dc:creator>Mick</dc:creator>
      <dc:date>2021-04-17T09:22:59Z</dc:date>
    </item>
    <item>
      <title>Re: Function in loop</title>
      <link>https://community.esri.com/t5/python-questions/function-in-loop/m-p/1048413#M60832</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 17 Apr 2021 09:23:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/function-in-loop/m-p/1048413#M60832</guid>
      <dc:creator>Mick</dc:creator>
      <dc:date>2021-04-17T09:23:27Z</dc:date>
    </item>
    <item>
      <title>Re: Function in loop</title>
      <link>https://community.esri.com/t5/python-questions/function-in-loop/m-p/1048416#M60833</link>
      <description>&lt;P&gt;Yes, like I said, you are &lt;U&gt;&lt;STRONG&gt;return&lt;/STRONG&gt;&lt;/U&gt;ing in your first loop iteration. So your function never gets to subsequent iterations.&amp;nbsp; Doesn't matter though, you don't need to loop in your makeStatesLayer function.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcpy import env  # outside function so it only gets called once
import arcpy

def makeStateLayer(folder, name):
    query = "StateField = '{0}'".format(name)
    #Create a feature layer containing all states
    statesLayer = arcpy.MakeFeatureLayer_management("State", "States_Layer",query)[0]
    return statesLayer&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;&lt;LI-CODE lang="python"&gt;import arcpy, makeStateLayer, Statesnames
from arcpy import env
pathway = "C/1.gdb"
env.workspace = pathway

States_names = Statesnames.Statesnames(pathway) 
for name in States_names:
    stateLayer = makeStateLayer.makeStateLayer(pathway, name)
    ..........................................
    #here I will delete the layer to get a new one at the beginning of the loop
    # next iteration of this loop, you will get a new layer with the next state name.&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 17 Apr 2021 09:20:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/function-in-loop/m-p/1048416#M60833</guid>
      <dc:creator>Luke_Pinner</dc:creator>
      <dc:date>2021-04-17T09:20:42Z</dc:date>
    </item>
  </channel>
</rss>

