Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Pythonic ways for checking for <code>None</code> or <code>null</code> are:</p> <pre><code>if element: # This is not null if not element: # This is null </code></pre> <p>There is a very <a href="https://stackoverflow.com/a/100762/1401034">detailed answer</a> on the difference between <code>if not x</code> and <code>if x == None</code>.</p> <h2>Edit 1:</h2> <p>Combining the comment and the other answers:</p> <h3>False Values</h3> <p>Python treats the following as <code>False</code> <a href="http://docs.python.org/2/library/stdtypes.html#truth-value-testing" rel="nofollow noreferrer">source</a>:</p> <ul> <li><code>None</code></li> <li><code>False</code></li> <li>zero of any numeric type, eg: <code>0</code>, <code>0L</code>, <code>0.0</code>, <code>0j</code></li> <li>any empty sequence, eg: <code>''</code>, <code>()</code>, <code>[]</code></li> <li>any empty mapping, eg: <code>{}</code></li> <li>instances of user-defined classes, if the class defines a <code>__nonzero__()</code> or<code>__len__()</code> method, when that method returns the integer <code>zero</code> or bool value <code>False</code></li> </ul> <h3>True Values</h3> <p>All other values are considered to be <code>True</code>.</p> <h3>Your question:</h3> <p>If you are indeed checking if <code>None</code> is present in your list (<code>[1,2,None,4,5,6]</code>) then @Poke 's answer is right:</p> <pre><code>&gt;&gt;&gt; lst = [1, 2, None, 4, 5, 6] &gt;&gt;&gt; None in lst True </code></pre> <p>If you are wanting to check that the element is <em>only</em> <code>None</code> then @esauro is correct in the comments:</p> <pre><code>&gt;&gt;&gt; lst = [1, 2, None, 4, 5, 6] &gt;&gt;&gt; for x in lst: ... if not x: ... print(x) None </code></pre> <p>But if your <code>lst</code> contains <code>0</code> (lst = [0, 1, 2, 4, 5, 6]) then your output will be <code>0</code>.</p> <p>The only way you could get round this would be to explicitly check <code>if element is None</code>or <code>if element is not None</code>. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload