dimanche 11 août 2019

How is it possible to check out for some particular elements in a list by some and/or conditions in one if statement

I have the list of tuples

a=[('H40T','H04H'), ('Z40A','H04T'), ('M90K','H04B'), ('H40H','H04B'), ('H40A','A04B'), ('A40T','Q04Q'), ('H40T','H04H'), ('Z40A','L04M'), ('A03C','H04T'), ('H40T','H04B'), ('H40A','A04B'), ('H40T','Q04C')]

and I am going to just keep the tuples that they have at least one of the elements desire=['H04B','H04T','H04H']

I tried to used this code, but it did not remove all the tuples without the mentioned elements.

for counter in a:
    if ('H04B' not in counter and 'H04T' not in counter and 'H04H' not in counter):
         a.remove(counter)

The problem is for the tuples have none of them. Also, I tried any() to find the unwanted elements.

for counter in a:
    if any(x in counter for x in desire):
       a.remove(counter)

Aucun commentaire:

Enregistrer un commentaire