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
Post a Comment