<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Python None, Null, and Zero in Geoprocessing Questions</title>
    <link>https://community.esri.com/t5/geoprocessing-questions/python-none-null-and-zero/m-p/611457#M20286</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Happy New Year...and one of the 'renewed' resolutions I have is to remember to correct more of my brain farts, such as this one in my above post:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;lambda x: x&amp;gt;=0&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;To be equivalent to what I posted in the preceding code block:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;i for i in theList if i is not None&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;...then I should have posted:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;lambda x: x is not None&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;(or, to repost the entire code with the correction):&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
&amp;gt;&amp;gt;&amp;gt; print f1, f2
0 None
 
&amp;gt;&amp;gt;&amp;gt; def trial(f1, f2):
 list = [f1, f2]
 l1 = len(filter(lambda x: x is not None, list))
 return l1

&amp;gt;&amp;gt;&amp;gt; trial(f1, f2)
1
&amp;gt;&amp;gt;&amp;gt;
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;That way, whatever was plugged into the initial solution, the list comprehension expression (including any text strings or negative numbers, for example), works in the same fashion with lambda x.&amp;nbsp; (My sample input, 0 and None, wasn't broad enough to show this.)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Just FYI...lol, don't know why I didn't just keep it consistent, like I said: brain fart!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Wayne&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 12 Dec 2021 02:09:25 GMT</pubDate>
    <dc:creator>T__WayneWhitley</dc:creator>
    <dc:date>2021-12-12T02:09:25Z</dc:date>
    <item>
      <title>Python None, Null, and Zero</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/python-none-null-and-zero/m-p/611454#M20283</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi There,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm performing some calculations in a table where I want to filter out Null values, but keep values of zero. I am trying to use the Python Filter function, but am having some issues. When I specify that values of "None" be filtered, zero values are also filtered. I would like to only filter Null values. This is confusing me because any literature I read says that the Python None value is equal to Null.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here's a simple form of my Python script (Pre-logic Script) in field calculator:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;def trial(f1,f2):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; list = [f1,f2]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; l1 = len(filter(None,list))&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; return l1&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The code above runs fine, but returns undesired lengths when f1 or f2 are zero.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Can anyone tell me what I am doing wrong, or another simple way of filtering out Null values and not zero values? Thanks for your insight!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Happy New Year!&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Nick&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 30 Dec 2013 15:56:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/python-none-null-and-zero/m-p/611454#M20283</guid>
      <dc:creator>NickLegg</dc:creator>
      <dc:date>2013-12-30T15:56:56Z</dc:date>
    </item>
    <item>
      <title>Re: Python None, Null, and Zero</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/python-none-null-and-zero/m-p/611455#M20284</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Think I'd just do it like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;def trial(f1,f2): &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; list = [f1,f2] &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; l1 = len([i for i in list if i is not None]) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return l1&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Wayne&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;PS - Incidentally, if you had an undetermined number of values, say f1, f2, f3, f4,... , then you could feed in the whole list, as in:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;def trial(theList): &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; l1 = len([i for i in theList if i is not None]) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return l1&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;...so that, for example, you could call it as such (this example simply has 3 items in a list):&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;&amp;gt;&amp;gt;&amp;gt; print list # a predefined list [0, None, 3] &amp;gt;&amp;gt;&amp;gt;&amp;nbsp; &amp;gt;&amp;gt;&amp;gt; trial(list) 2 &amp;gt;&amp;gt;&amp;gt; &lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;...and, since I've never actually used 'filter' or 'lamda' before, here is another alternative - and I'm not sure I'd use this or if it can be written shorter, but certainly an alternative (which can also be rewritten to accept a list):&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;&amp;gt;&amp;gt;&amp;gt; print f1, f2 0 None&amp;nbsp;&amp;nbsp; &amp;gt;&amp;gt;&amp;gt; def trial(f1, f2):&amp;nbsp; list = [f1, f2]&amp;nbsp; l1 = len(filter(lambda x: x&amp;gt;=0, list))&amp;nbsp; return l1&amp;nbsp; &amp;gt;&amp;gt;&amp;gt; trial(f1, f2) 1 &amp;gt;&amp;gt;&amp;gt; &lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 30 Dec 2013 16:29:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/python-none-null-and-zero/m-p/611455#M20284</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2013-12-30T16:29:33Z</dc:date>
    </item>
    <item>
      <title>Re: Python None, Null, and Zero</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/python-none-null-and-zero/m-p/611456#M20285</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;That worked perfect! Thanks Wayne.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 30 Dec 2013 16:37:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/python-none-null-and-zero/m-p/611456#M20285</guid>
      <dc:creator>NickLegg</dc:creator>
      <dc:date>2013-12-30T16:37:43Z</dc:date>
    </item>
    <item>
      <title>Re: Python None, Null, and Zero</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/python-none-null-and-zero/m-p/611457#M20286</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Happy New Year...and one of the 'renewed' resolutions I have is to remember to correct more of my brain farts, such as this one in my above post:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;lambda x: x&amp;gt;=0&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;To be equivalent to what I posted in the preceding code block:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;i for i in theList if i is not None&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;...then I should have posted:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;lambda x: x is not None&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;(or, to repost the entire code with the correction):&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
&amp;gt;&amp;gt;&amp;gt; print f1, f2
0 None
 
&amp;gt;&amp;gt;&amp;gt; def trial(f1, f2):
 list = [f1, f2]
 l1 = len(filter(lambda x: x is not None, list))
 return l1

&amp;gt;&amp;gt;&amp;gt; trial(f1, f2)
1
&amp;gt;&amp;gt;&amp;gt;
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;That way, whatever was plugged into the initial solution, the list comprehension expression (including any text strings or negative numbers, for example), works in the same fashion with lambda x.&amp;nbsp; (My sample input, 0 and None, wasn't broad enough to show this.)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Just FYI...lol, don't know why I didn't just keep it consistent, like I said: brain fart!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Wayne&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 02:09:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/python-none-null-and-zero/m-p/611457#M20286</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2021-12-12T02:09:25Z</dc:date>
    </item>
  </channel>
</rss>

