Reputation: 59
a = ['ava','olivia','hannah','olivia']
b = ['aa','ab','ac','av']
for i in a:
for j in b:
if j in i:
print (i)
#output: ava
for i in a:
if any(j in i for j in b):
print (i)
#output: ava
There are 2 lists with some elements, I want to the if each list 2 elements in each list 1 elements.
There are any better way to get it?
Upvotes: 0
Views: 67
Reputation: 20777
This is enough:
print [word for word in a if any(part in word for part in b)]
No need to use filter
, map
or lambda
.
Upvotes: 5
Reputation: 6796
Not necessarily better, but they're oneliners:
a = ['ava','olivia','hannah','olivia']
b = ['aa','ab','ac','av']
result = filter(None, map(lambda x: x if any(map(lambda y: y in x, b)) else None, a))
print result
or:
result2 = filter(None, [x if y in x else None for x in a for y in b])
print result3
or:
result3 = filter(None, [x if any([y in x for y in b]) else None for x in a])
print result2
Upvotes: 0