Function in loop

940
4
Jump to solution
04-16-2021 11:02 PM
Mick
by
New Contributor

Could you please correct my function of the main script in order for the new call to list the next value in the list?

I am definitely sure that the problem in my function because it lists only the first value in States_name list.

 

0 Kudos
1 Solution

Accepted Solutions
Luke_Pinner
MVP Regular Contributor

Yes, like I said, you are returning in your first loop iteration. So your function never gets to subsequent iterations.  Doesn't matter though, you don't need to loop in your makeStatesLayer function.

 

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

 

 

 

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.

 

View solution in original post

4 Replies
Luke_Pinner
MVP Regular Contributor

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).  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.

Try something like:

makeStateLayer.py

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

 

main_script.py 

from makeStateLayer import makeStateLayer

for name in States_names:
    stateLayer = makeStateLayer(pathway, name)

 

 

0 Kudos
Mick
by
New Contributor

 

 

 

 

 

 

 

 

0 Kudos
Mick
by
New Contributor

 

 

0 Kudos
Luke_Pinner
MVP Regular Contributor

Yes, like I said, you are returning in your first loop iteration. So your function never gets to subsequent iterations.  Doesn't matter though, you don't need to loop in your makeStatesLayer function.

 

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

 

 

 

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.