Q:

Python: recurse through list of strings - how to tell the difference?

I have the following code
def printmylist( mylist ):
    """
    print tree
    """
    try:
    for f in mylist:
        printmylist( f )
    except:
        print( "        " + mylist )

hoping to get output like:
root
   branch
     leaf
     leaf

but since a string is enumerable, I get
r
o
o
t
.
.

Checking for type seems to be unpythonic, so how would a Pythonian go about this?

A:

The cleanest way that I know of is to use types.StringTypes (not to be confused with types.StringType):
isinstance(var, types.StringTypes)

Alternatively:
isinstance(var, basestring)

Documentation for the types module indicates that the latter is the preferred way in recent versions of Python 2.x.

