Given two lists list1 and list2 of booleans , I want to extend list1 by the complements of the elements in list2. For example if
list1 = [True, True, False]
list2 = [False, False, True, False]
then after the operation
list1 = [True, True, False, True, True, False, True]
while list2 shall remain unchanged.
What is the most pythonic way to achieve that?
>Solution :
How about this:
list1.extend(not value for value in list2)
If there is a risk that list2 is an alias to list1, you are better off with
list1 += [not value for value in list2]