Here's one way to test if the loop you just came out of was empty:
for item in range(0): print item else: if "item" not in locals(): print "Empty list."
Obviously, this only works if "item" has not been used in the current scope before. If anyone knows of a more efficient way to check if a variable name has been used besides using locals(), let me know.
Another way to do it is if you know that some particular value, such as None, will never appear in your list-like-object you can set item to that value in advance:
item = None for item in range(0): print item else: if item is None: print "Empty list."
Again, this has the limitation that it only works if you can guaranty that your guard value won't be used by the loop. The advantage is that you don't have to create locals() and then do a __contains__ on it, which presumably takes longer than a simple assignment and identity check.
-- Carl Johnson