9

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?

Bleeding Fingers
  • 6,993
  • 7
  • 46
  • 74
scopedial
  • 203
  • 1
  • 3
  • 7

4 Answers4

10

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 integer zero or bool value False

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 Noneor if element is not None.

Community
  • 1
  • 1
Ewan
  • 14,592
  • 6
  • 48
  • 62
  • 2
    This 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
  • Thank you @Ewan. Much more complete answer now. – esauro Oct 08 '13 at 20:13
6

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.

poke
  • 369,085
  • 72
  • 557
  • 602
6

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.

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
1

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]
Emil Davtyan
  • 13,808
  • 5
  • 44
  • 66