Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

extending list of booleans by complements of another list of booleans

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading