Suppose I have a list: [9,0.0,0,9,1,2,0,1,0,1,0.0,3,0,1,9,0,0,0,0,9,False]
and I want to move all zeros to the end.
I know I can use:
s
Since bool
is a subclass of int
and False == 0
is True (indeed, the success of our sorted
key
function depends on this), if you wish to treat False
as non-zero, then you'll need to add that as another condition:
sorted([9,0.0,0,9,1,2,0,1,0,1,0.0,3,0,1,9,0,0,0,0,9,False],
key=lambda x: (x == 0) and x is not False)
yields
[9, 9, 1, 2, 1, 1, 3, 1, 9, 9, False, 0.0, 0, 0, 0, 0.0, 0, 0, 0, 0, 0]