append zero but not False in a list python
<p>I'm trying to move all zeros in a list to the back of the line, my only problem is there is a False bool in the list. I just found out that False == 0, so how do i move all zeros to the back of the list and keep false intact?</p>

<pre><code>def move_zeros(array):
    #your code here
    for i in array:
        if i == 0:
            array.remove(i)
            array.append(i)
    answer = array
    print answer
</code></pre>

<p><code>move_zeros(["a",0,0,"b",None,"c","d",0,1,False,0,1,0,3,[],0,1,9,0,0,{},0,0,9])</code></p>

<p>This is what it returns when you run it.</p>

<pre><code>['a', 'b', None, 'c', 'd', 1, 1, 3, [], 1, 9, {}, 9, 0, 0, 0, 0, 0, False, 0, 0, False, 0, 0]
</code></pre>

<p>What you're doing is basically a custom sort.  So just implement it this way:</p>

<pre><code>array.sort(key=lambda item: item is 0)
</code></pre>

<p>What this means is "Transform the array into a boolean one where items which are 0 are True and everything else is False."  Then, sorting those booleans puts all the False values at the left (because they are like 0), and the True values at the right (like 1).</p>

<hr>

<p>Originally I had written a solution which is not supported in Python 3:</p>

<pre><code>array.sort(lambda L,R: -1 if R is 0 else 0)
</code></pre>

<p>What this means is "L is less than R if R is 0".  Then we sort according to that.  So we end up with any zeros on the right, because anything is less than them.  The above only works in Python 2, however.</p>

python list