Check if something is a list

Federer picture Federer · Apr 15, 2010 · Viewed 19.8k times · Source

What is the easiest way to check if something is a list?

A method doSomething has the parameters a and b. In the method, it will loop through the list a and do something. I'd like a way to make sure a is a list, before looping through - thus avoiding an error or the unfortunate circumstance of passing in a string then getting back a letter from each loop.

This question must have been asked before - however my googles failed me. Cheers.

Answer

Ants Aasma picture Ants Aasma · Apr 15, 2010

To enable more usecases, but still treat strings as scalars, don't check for a being a list, check that it isn't a string:

if not isinstance(a, basestring):
    ...