nonzero
is not a great name for this function. It implies a check to see if something is nonzero, not a simple sort method. It is hard to name something like this, but even the overly long zeroes_to_end
would be clearer as it's not ambiguously implying something else. Likewise a
and x
aren't great, instead maybe source
and result
? (assuming you don't decide to modify the original list as Gareth Rees suggested)
Also instead of:
zeros = [0 for i in range(a.count(0))]
It'd be easier to just do this:
zeros = [0] * a.count(0)
which will give you the same thing.
Lastly you don't need parentheses around your return
value. You can just do return a
which reads as more Pythonic.