lundi 7 décembre 2020

Python compare two lists, if words match add to another list

Im trying to compare 'words' with 'if_contains', when they match they should be added into save list. Expected output - ['one', 'two eight nine']. The output im getting - ['one']

if_contains = ['one', 'two', 'three']
save = []
words = ['one', 'five', 'six', 'two eight nine']

for word in words:
    if word in if_contains:
        save.append(words)

print(save)

Aucun commentaire:

Enregistrer un commentaire