[Tutor] Treating a string as a list of one?

Clay Shirky clay at shirky.com
Mon Dec 29 21:51:55 EST 2003


I want to be able to pass a function either a string or a list, and have it
treat the string as a list of one item.

I thought this would work

try:
        _loop.extend(items)
except:
        _loop.append(items)

for item in items:
    # other stuff here

thinking that when .extend got something that wasn't a list, it would raise
an exception. Instead, it treats the string as a list of chars.

Is there a simple way to say "if its a list, extend the _loop variable, and
if its a string, append it?"

-clay




More information about the Tutor mailing list