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.

I need to display a list of feature classes with an unknown spatial reference, and the number of feature classes with a spatial reference. I think I have the first part right, but having troubles with the second part. I've been playing around with the len() and count() but those always result in an error. The following is part of my code:

for fC in fCList:
     desc = arcpy.Describe(fC)
     spatialRef = desc.spatialReference
     if spatialRef.Name == 'Unknown': # List of feature classes that are unknown
        print "\n" + fC, ("has an 'Unknown' spatial reference\n")
     else: # Number of feature classes that have a spatial reference
        print ("Total number of feature classes with a spatial reference:"), count(fC)
share|improve this question
This seems like a pure Python question... have you gone through the tutorial? –  blah238 Nov 21 '11 at 7:05
The gist of the issue here is that you aren't keeping a persistent variable to count feature classes with/without a spatial reference. Majgis's solution below is a very pythonic method for doing so, but you could just as easily (or perhaps more easily; list comprenhensions give me a headache) use your else clause to increment a variable counting fc's with a spatial reference and then subtract it from the length of the fCList when your loop completes. –  Nathanus Nov 21 '11 at 13:38
@Nathanus Thanks for your help! I shall try it out! –  kolor Nov 21 '11 at 17:40
add comment

1 Answer

up vote 8 down vote accepted
unknowns = [fc for fc in fCList if arcpy.Describe(fc).spatialReference.Name == "Unknown"]

knownCount = len(fCList) - len(unknowns)

print "Known Total:", knownCount
print "Unknowns:\n   ", "\n    ".join(unknowns)

If you are unsure how the unknowns list is created, see List comprehensions.

share|improve this answer
Wow, I had no idea you could use for/if statements in lists. Sorry, I'm new to Python. Thanks for your help @majgis! –  kolor Nov 21 '11 at 17:39
List comprehensions are handy, but be careful, they are hard to read and debug if they are too complex. Also, realize we're storing a list in memory, which is overkill for your simple example, but more versatile for a structured approach (pass this list to a series of functions if you decide to take additional steps). –  majgis Nov 23 '11 at 22:49
add comment

Your Answer


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.