mercredi 17 juin 2020

Method or function application on list of strings with if conditions

I have two lists as follows:

a = ["aaa", "bbb", "ccc"]
b = ["aaa", "bbb", "ddd", "eee"]

I would like to have a print (a function or a method more in general) of the a[i] elements if a[i]==b[j]. This is the code I wrote, it doesn't work because it returns four times the condition.

for i in range(len(a)):
    for j in range(len(b)):
        if a[i] == b[i]:
            print (a[i])

Aucun commentaire:

Enregistrer un commentaire