jeudi 28 mai 2020

Python searching lists with lists, all and any

I am having trouble for some reason with a certain if statement. Let's say I have three lists:

list1 = [1,2,3,4,5]
list2 = [1,2,4,5]
list3 = [4,5,6,7,8]

I evaluate each. I want to flag it if the list has both 4 AND 5, but does not have either 2 OR 3. I thought this would work:

if all(q in [4,5] for q in the_list_name) and not any(q in [2,3] for q in the_list_name)

I would expect this to flag only list3. However it returns false on all of the lists. I wonder what I am doing wrong?

Aucun commentaire:

Enregistrer un commentaire