Skip to main content
5 of 5
Title English
Vince
  • 20.3k
  • 16
  • 48
  • 65

List of small lists in Python: How to retrieve items from small list in a loop?

I would like to automate the process of making some certain feature layers.

I made a list of lists that goes like that: [[feature class, subtype],[feature class,subtype],...]

I'd like to loop through the list and make feature layers using the items in the smaller lists for output paths and queries. So far I wasn't able to retrieve them correctly.

bigNeighborList=[["ExtractA",0],["ExtractA",1], ["BuiltupA",0],["CoastA",3],["SwampA",0],["WatrcrsA",0],["WatrcrsA",1],["LakeresA",0]] #["LakeresA",2],["LakeresA",1],["WatrcrsA",2],["GroundA",0],["Landfrm1A",1],["CropA",1],["CropA",2],["GrassA",0],["TreesA",1],["TreesA",0], ["FirebrkA",0],["SwampA",1],["SwampA",2],["CropA",3]]
for itemA in bigNeighborList:   
    bigFC = [itemA[0] for itemA in bigNeighborList] #retrieve the first item - feature class name from a smaller list
    subtype = [itemA[1] for itemA in bigNeighborList] #retrieve the second item - subtype number
    outputItemA = os.path.join(wrksp100, str(bigFC[0]))
    print(outputItemA)
    qry = "%s = %d " % (subField,int(subtype[0])) ##query to choose the right subtype
    tempLyr = arcpy.MakeFeatureLayer_management(outputItemA,str(bigFC)+"_lyr",qry) #temporary layer for every list item

the print statement returns the path "C:\DAR...3\ExtractA" 8 times for every itemA in the list and creates new ExtractA tempLyr (though named with all first items from small lists - "['ExtractA', 'ExtractA', 'BuiltupA', 'CoastA', 'SwampA', 'WatrcrsA', 'WatrcrsA', 'LakeresA']_lyr") over and over.

Is something wrong with my indexing?

I'm stuck.