python sorting list by true or false -


i have list of posts. each post has ture or false value determines in list post should be. true posts should @ beginning of list , false posts should @ end. how can sort list that? example:

posts[0].value = true posts[1].value = false posts[2].value = false posts[3].value = true 

should sorted this: post[0], post[3], post[1], post[2]

thanks in advance

you can this:

posts.sort(key=lambda x: not x.value) 

key used specify custom function compare items in list. function switches value because in python false < true.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -