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.
1 Answer 1
The problem is in the fact that what you wrote is different from what you think you wrote. The comment says "retrieve the first item", but what really happens is "build a list that consists of the first item of every entry in the big list".
bigFC = [itemA[0] for itemA in bigNeighborList] #retrieve the first item
subtype = [itemA[1] for itemA in bigNeighborList] #retrieve the second item
After the execution of these lines, bigFC
will be a list like [ExtractA, ExtractA, BuildUpA, ...]
, while subtype
will be a list like [0, 1, 0, 3, 0, ...]
. This technique is called a list comprehension, it is very useful, but it is probably not what you want to use here.
If you want just the first element of the list (rather than a list), use this:
bigFC = itemA[0] # REALLY retrieve the first item
subtype = itemA[1] # REALLY retrieve the second item
- It helps to add
print
statements after some lines, to check the values of the variables and make sure they match your expectations. - A more sophisticated approach would be to learn how to use Python's built-in debugger,
pdb
; or rely on your IDE to show you the state of your program as it is executed line by line. - Consider using tuples instead of lists (for the small items), if you're sure that the "small lists" will only ever contain 2 elements. In that case you might write your code as
for bigFC, subtype in bigNeighborList: ...
, which is more readable.
Explore related questions
See similar questions with these tags.
print
statement to show in the first iteration?