Search code examples
pythonlist

How to change all boolean values in a list at once


I have a boolean list consist of True and False as follows. I want to know how to change all the values ​​in a list to True or False at once.

my_list = [ False, True, False ]
print(my_list)

my_list[:] = True

The last line throws an exception error, which is not what I expected.


Solution

  • my_list = [False, True, False]
    
    my_list[:] = [True] * len(my_list)
    
    print(my_list)