Test 0 and false since false is 0

Paul D. DeRocco pderocco at ix.netcom.com
Sat Jul 8 22:36:18 EDT 2017


> From: Sayth Renshaw
> 
> I have been reading this solution 
> > >>> after = sorted(before, key=lambda x: x == 0 and type(x) == int)
> 
> it is really good, however I don't understand it enough to 
> reimplement something like that myself yet.
> 
> Though I can that lambda tests for 0 that is equal to an int 
> why does sorted put them to the end?

Because the expression "x == 0 and type(x) == int" has a value of either
False or True, and it sorts all the False values before the True values,
leaving the order within those sets unchanged.

That said, "x is 0" is even simpler.

-- 

Ciao,               Paul D. DeRocco
Paul                mailto:pderocco at ix.netcom.com




More information about the Python-list mailing list