I'm new to python and haven't yet read a lot of code to verify which styles are considered 'pythonic'.

As I've started to code, I've been using this pattern alot.

listThatMightBeEmpty = []
for items in listThatMightBeEmpty:
print "this may or may not print but the loop won't cause any errors"

I assume that it would be redundant to first check if the list is empty - that I can just let the for-in loop do nothing if the list is empty. But I want to make sure there aren't any gotchas for any situation including using old versions of python, and including using a for-in loop on dictionaries, using enumerate(), or iteritems(), or using list comprehensions etc.

I tend to keep an interactive python session open in a different window while I'm coding, so if I am curious about a particular case, I can just experiment as I go. This particular question, though, is one you want to know generally, and the answer is, pretty much, no gotchas. It's perfectly possible to create an object that acts like a list except it raises an exception if accessed when empty, or returns the string "418 I'm a teapot", or something else surprising, but that would be a very unusual and unpythonic case.
–
Russell BorogoveOct 3 '10 at 18:57

Yes it is redundant (unless you want to perform something special for empty input).

In general, the for loop and and function that return another thing that you can iterate on will not give any "surprising behaviors" if you have an input of zero items. This includes enumerate() , iteritems(), and even slicing ([][1:] == []).