Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

this gives me a list of all 55 layers in my mxd

import arcpy 
mxd = arcpy.mapping.MapDocument("CURRENT") 
df = arcpy.mapping.ListDataFrames(mxd, "Layer")[0] 
for lyr in arcpy.mapping.ListLayers(mxd, "*", df):
    print lyr.name

What I want to do is get the index number of a specific layer. For example, I want to look for the layer named 'Water' and I want the script to tell me "Water' is at position 50, so I know to look for it at the bottom on the TOC. How can I do this?

share|improve this question

3 Answers 3

up vote 4 down vote accepted

What you are looking for is enumerate.

import arcpy 
mxd = arcpy.mapping.MapDocument("CURRENT") 
df = arcpy.mapping.ListDataFrames(mxd, "Layer")[0] 
for i,lyr in enumerate(arcpy.mapping.ListLayers(mxd, "*", df)):
    print "Layer {0}: {1}".format(i, lyr.name)
share|improve this answer
    
Enumerate pointed me in the right direciton –  detroit_hc Jul 6 '13 at 21:57

I would use enumerate.

>>> x = ['a','b','c']
>>> for index,y in enumerate(x):
...     print index,y
...
0 a
1 b
2 c

Or from your snippet:

for i,lyr in enumerate(arcpy.mapping.ListLayers(mxd, "*", df)):
    print i,lyr.name
share|improve this answer
    
Enumerate pointed me in the right direction. I just had to add a conditional if statement to do what I wanted to do - find the position of a specific layer... if lyr.name == "Land Use": before the print line. –  detroit_hc Jul 6 '13 at 21:52

You could use the index() method:

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT") 
df = arcpy.mapping.ListDataFrames(mxd, "Layer")[0]
lyrNameList = []
for lyr in arcpy.mapping.ListLayers(mxd, "*", df):
    lyrNameList.append(lyr.name)
print lyrNameList.index("Land Use")
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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