John Arne Holt
John Arne Holt

Reputation: 1

making loop script that needs to remove list elements as it goes thru

Python problem

import fnmatch

list_1 = ['family', 'brother', 'snake', 'famfor']

list_2 = ['a', 'f', 'f', 'm', 'i', 'l', 'y']

match = fnmatch.filter(list_1, 'fa????')

print match

This would give me

>> ['family', 'famfor']

how can i only get family in this query? by checking list_2 for valid letters.

Upvotes: 0

Views: 73

Answers (1)

blhsing
blhsing

Reputation: 107115

You can first convert list_2 to a set for efficient lookups, and then use a list comprehension with a condition as a filter:

set_2 = set(list_2)
[w for w in list_1 if all(c in set_2 for c in w)]

This returns:

['family']

Upvotes: 1

Related Questions