Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Check if an object is a list of strings

How to check if an object is a list of strings? I could only check if an object is string as such:

def checktype(obj):
  if isinstance(obj,str):
    print "It's a string"

obj1 = ['foo','bar','bar','black','sheet']
obj2 = [1,2,3,4,5,'bar']
obj3 = 'bar'

for i in [obj1,obj2,obj3]:
  checktype(i)

Desired output:

It's a list of strings
It's not a list of strings or a single string
It's a single string
like image 348
alvas Avatar asked Aug 28 '13 18:08

alvas


People also ask

How do you check if an object is a list of lists Python?

isinstance(i, list) is the better way of writing type(i) == type(list) or type(i) == list) . all() returns True if all of the items in the sequence are True . It'll return False if any aren't True .

How do you check if an item in a list is a string Python?

Python Find String in List using count() We can also use count() function to get the number of occurrences of a string in the list. If its output is 0, then it means that string is not present in the list.

How do you check if an element has a list?

The most convenient way to check whether the list contains the element is using the in operator. Without sorting the list in any particular order, it returns TRUE if the element is there, otherwise FALSE.


1 Answers

Something like this, I presume? You could do some checks to see if it's a single string.

>>> def checktype(obj):
        return bool(obj) and all(isinstance(elem, basestring) for elem in obj)

>>> obj1 = ['foo','bar','bar','black','sheet']
>>> obj2 = [1,2,3,4,5,'bar']
>>> obj3 = 'bar'
>>> for i in [obj1, obj2, obj3] :
        print checktype(i)


True
False
True

Why check for basestring instead of str?

You should check for basestring instead of str since it's a common class from which both the str and unicode types inherit from. Checking only the str leaves out the unicode types.

As per Steven Rumbalski's suggestions, if you need to specifically check for a list of strings, you could do.

>>> def is_list_of_strings(lst):
        return bool(lst) and not isinstance(lst, basestring) and all(isinstance(elem, basestring) for elem in lst)
        # You could break it down into `if-else` constructs to make it clearer to read.

>>> for i in [obj1, obj2, obj3] :
        print is_list_of_strings(i)


True
False
False

EDIT - As per abarnert's suggestion, you could also check for a list instead of not isinstance(lst, basestring), the code would get rewritten as.

>>> def is_list_of_strings(lst):
        return bool(lst) and isinstance(lst, list) and all(isinstance(elem, basestring) for elem in lst)
        # You could break it down into `if-else` constructs to make it clearer to read.

>>> for i in [obj1, obj2, obj3] :
        print is_list_of_strings(i)


True
False
False

Moving away from one liners, we could use.

>>> def is_list_of_strings(lst):
        if lst and isinstance(lst, list):
            return all(isinstance(elem, basestring) for elem in lst)
        else:
            return False
like image 122
Sukrit Kalra Avatar answered Sep 20 '22 20:09

Sukrit Kalra