I have a list of bool
s and I want to convert it to a single int
. For example: [True, True, False, True, False]
-> 010112 -> 1110
I found this question: How do I convert a boolean list to int? but it's in C# and that doesn't help me.
My first try was to do
def bools2int(bools):
n = 0
for b in bools[::-1]:
n *= 2
n += b
return n
which works.
But is there a more pythonic way to do this? And can it be done in one line?