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:\)

#------------------------------------------------- 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

outBacklink = CostBackLink("Hiker.shp","outPlus1030", 100000, 


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

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


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

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


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.

1 Answer 1


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)
    outPlus = Plus('outTimes.tif', 'outland.tif')
    outBacklink = CostBackLink("Hiker.shp","outplus", 100000, 
    #More code here
  • 1
    Never used that zip() function, nice example bringing 3 values together, thanks!
    – Hornbydd
    Nov 11, 2018 at 19:14
  • I get the error zip argument #1 must support iteration. How to fix this error? I never tried this before. @BERA
    – random123
    Nov 12, 2018 at 22:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.