# Using for loop in least cost path analysis using ArcPy?

I am doing a least cost path analysis. I need to create a for loop that will run 3 times and select the correct weight from each of the two lists (slope and land).

``````Slope = [10, 20, 30]
Land = [30, 20, 10]

import arcpy
from arcpy import env
from arcpy.sa import *
env.workspace = (c:\)
arcpy.CheckOutExtension("Spatial")

#------------------------------------------------- Times calculation

outTimes10 = Times("Slope",10)
outTimes20 = Times("Slope",20)
outTimes30 = Times("Slope",30)

#------------------------------------------------- Times calculation

outland30 = Times("land_tif", 30)
outland20 = Times("land_tif", 20)
outland10 = Times("land_tif", 10)

#------------------------------------------------- Added two raster together

outPlus1030 = Plus("outTimes10", "outland30")
outPlus2020 = Plus("outTimes20", "outland20")
outPlus3010 = Plus("outTimes30", "outland10")

#------------------------------------------------- Cost Back link

"c:/sapyexamples/output/distRast")

pair1bl="CostBac_shp1"
pair2bl="CostBac_shp2"
pair3bl="CostBac_shp3"

#------------------------------------------------- Cost distance

outCostDist = CostDistance("Hiker.shp", "slope", 200000, "pair1bl")

pair1cd="CostDis_shp1"
pair2cd="CostDis_shp2"
pair3cd="CostDis_shp3"

#------------------------------------------------- Least cost path

outCostPath = CostPath("Hiker.shp", "outplus1030", "pair1bl", "EACH_CELL")

path1="CostPat_shp1"
path2="CostPat_shp2"
path3="CostPat_shp3"
``````

I am new to python. I need help with creating a for loop that will select numbers from the two list do the times calculation, plus calculation, cost backlink, cost distance, and least cost path.

I do not know how to direct the output of the calculation to be saved in a different file name everytime the loop runs.

You can zip the lists together and iterate over each slopevalue, landvalue and a name:

``````#More code above
arcpy.env.overwriteOutput = True

Slope = [10, 20, 30]
Land = [30, 20, 10]
Name = ['Out1','Out2','Out3']

for slopeval,landval,name in zip(Slope,Land,Name):
outTimes = Times("Slope",slopeval)
outTimes.save('outTimes.tif') #Dont know if saving is required or if it is possible to use the raster object as input to next steps.
outland = Times("land_tif", landval)
outland.save('outland.tif')
outPlus = Plus('outTimes.tif', 'outland.tif')
outPlus.save('outplus.tif')