Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I test for null list entry in python list

Tags:

python

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?

like image 970
scopedial Avatar asked Oct 08 '13 15:10

scopedial


People also ask

How check if list contains empty list Python?

You can use the len() function with the comparison operator and compare the result with 0 to check if the list is empty. What is this? If the list is empty, then the If statement will be executed. This is how you can use the len() function with the comparison operator to check if the list is empty or not in python.

How do you check if an element in a list is empty?

Checking empty list using len() Function. The len() function is used to find the number of elements in the list. So, to check if the list is empty or not using len(), we can pass the empty list to the len() function, and if we get 0, that means the list is empty.

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

We can use the in-built python List method, count(), to check if the passed element exists in the List. If the passed element exists in the List, the count() method will show the number of times it occurs in the entire list. If it is a non-zero positive number, it means an element exists in the List.


3 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 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.

like image 186
Ewan Avatar answered Oct 01 '22 10:10

Ewan


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.

like image 41
poke Avatar answered Oct 01 '22 11:10

poke


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.

like image 45
Burhan Khalid Avatar answered Oct 01 '22 12:10

Burhan Khalid