Say I have the following array:
[True, True, True, True]
How do I toggle the state of each element in this array?
Toggling would give me:
[False, False, False, False]
Similarly, if I have:
[True, False, False, True]
Toggling would give me:
[False, True, True, False]
I know the most straightforward way to toggle a boolean in Python is to use "not" and I found some examples on stackexchange, but I'm not sure how to handle it if its in an array.
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]
>>>