How do I toggle a boolean array in Python?
Using not
is still the best way. You just need a list comprehension to go with it:
>>> x = [True, True, True, True]>>> [not y for y in x][False, False, False, False] >>> x = [False, True, True, False]>>> [not y for y in x][True, False, False, True]>>>
I'm pretty sure my first solution is what you wanted. However, if you want to alter the original array, you can do this:
>>> x = [True, True, True, True]>>> x[:] = [not y for y in x]>>> x[False, False, False, False]>>>