How can I verify if the content of a variable is a list or a string?

Terry Reedy tjreedy at udel.edu
Tue Jan 31 19:56:55 EST 2012


On 1/31/2012 7:44 PM, Andres Soto wrote:
> Hi,
> I'm writing a function which receive a list which elements are strings
> or new lists (sublists) containing strings.
> How can I verify if sone element of the list (which is contained in a
> variable) is a list or a string?
> I found the method isinstance(object,class) but I don't know which class
> should I use for.

For 3.x, 'list' or 'str' (where 'str' means unicode). For 2.x, I believe 
'basestring' includes unicode strings as well as byte strings.

 >>> isinstance([], list)
True
 >>> isinstance([], str)
False

-- 
Terry Jan Reedy




More information about the Python-list mailing list