I have a python list [1,2,,4,5,6]
. What is the best method for testing for the missing, or null
, list element? I am currently performing a if element != ''
but I think there is a built-in test to perform such a thing, or am I wrong?

- 6,993
- 7
- 46
- 74

- 203
- 1
- 3
- 7
-
8I tried entering `[1,2,,4,5,6]` into my interpreter and got a `SyntaxError`. Can you show your actual code? – Kevin Oct 08 '13 at 15:58
-
`if element == None`? – Marco Scannadinari Oct 08 '13 at 16:00
4 Answers
Pythonic ways for checking for None
or null
are:
if element:
# This is not null
if not element:
# This is null
There is a very detailed answer on the difference between if not x
and if x == None
.
Edit 1:
Combining the comment and the other answers:
False Values
Python treats the following as False
source:
None
False
- zero of any numeric type, eg:
0
,0L
,0.0
,0j
- any empty sequence, eg:
''
,()
,[]
- any empty mapping, eg:
{}
- instances of user-defined classes, if the class defines a
__nonzero__()
or__len__()
method, when that method returns the integerzero
or bool valueFalse
True Values
All other values are considered to be True
.
Your question:
If you are indeed checking if None
is present in your list ([1,2,None,4,5,6]
) then @Poke 's answer is right:
>>> lst = [1, 2, None, 4, 5, 6]
>>> None in lst
True
If you are wanting to check that the element is only None
then @esauro is correct in the comments:
>>> lst = [1, 2, None, 4, 5, 6]
>>> for x in lst:
... if not x:
... print(x)
None
But if your lst
contains 0
(lst = [0, 1, 2, 4, 5, 6]) then your output will be 0
.
The only way you could get round this would be to explicitly check if element is None
or if element is not None
.
-
2This is not exact. If `element` is 0 is not null, neither None, but `not element` would evaluate as True. In the same way if element is `[]` (an empty list) it would evaluate to True. Thus you should test `element is None` or `not element is None` – esauro Oct 08 '13 at 16:52
-
Thank you @esauro - i've updated my answer based on your comment and Pokes answer – Ewan Oct 08 '13 at 18:11
-
If your list is [1, 2, None, 4, 5, 6]
then you can just check None in lst
, e.g.:
>>> lst = [1, 2, None, 4, 5, 6]
>>> None in lst
True
Similarly, if you have some other value that represents a “missing” entry, you can just check whatever in lst
.

- 369,085
- 72
- 557
- 602
-
I liked this one because it doesn't create a new list in the process of checking. – bcollins Oct 08 '13 at 16:34
I have a python list [1,2,,4,5,6]. What is the best method for testing for the missing, or null, list element?
If you just want to know if the list contains a falsey value (that's the best equivalent for missing or null in Python), you can do this:
>>> all(x for x in [1,2,3,4,5])
True
>>> all(x for x in [1,2,'',4,5])
False
>>> all(x for x in [1,2,None,4,5])
False
If you want to know if a specific value exists that matches some condition:
all(x%2 for x in [1,2,3,4,5,7])
This will be true if the list contains all even numbers.

- 169,990
- 18
- 245
- 284
-
You do not need the `x for x in` part. Also zero will treated as false. – dansalmo Oct 09 '13 at 01:13
-
You need the `x for x` if you want to test for something other than true or false. – Burhan Khalid Oct 09 '13 at 04:25
Find all the indexes where there is a None
:
[i for i, item in enumerate(mylist) if item is None]
Example output :
>>> mylist = [1, 2, None, 4, 5, 6, None, None]
>>> [i for i, item in enumerate(mylist) if item is None]
[2, 6, 7]

- 13,808
- 5
- 44
- 66
-
1Note that `None` should be compared using identity, not equality: `if item is None`... – Jon Clements Oct 08 '13 at 16:04
-
@JonClements Can you point me to an explanation? I'm new to python. – Emil Davtyan Oct 08 '13 at 16:05