tags:

views:

150

answers:

4
l=[None,None]

i want to know is there any function to check list l is None or not.....

+2  A: 

Try any() - it checks if there is a single element in the list which is considered True in a boolean context. None evaluates to False in a boolean context, so any(l) becomes False.

Note that, to check if a list (and not its contents) is really None, if l is None must be used. And if not l to check if it is either None (or anything else that is considered False) or empty.

Alexander Gessler
+7  A: 

If you mean, to check if the list l contains only None,

if all(x is None for x in l):
  ...
KennyTM
"...contains only None" OR is empty.
Constantin
@system: Why not?
KennyTM
@systempuntoout, this is actually a generator expression.
Constantin
+5  A: 
L == [None]*len(L)

is much faster than using all() when L is all None

$ python -m timeit -s'L=[None]*1000' 'all(x is None for x in L)'
1000 loops, best of 3: 276 usec per loop
$ python -m timeit -s'L=[None]*1000' 'L==[None]*len(L)'
10000 loops, best of 3: 34.2 usec per loop
gnibbler
A: 

If you want to check if the members of the list are None, then you can loop over the items and check if they are None

If you want to check if list itself is None, you can use type(varlist) and it will return None

you can do

if (lst == None): ... print "yes"

works.

powerrox