itthrill
itthrill

Reputation: 1376

Return sub list of elements based on matching sub strings from another list

I want to filter a list of string based on another list of sub strings.

main_list=['London','England','Japan','China','Netherland']
sub_list=['don','land']

I want my result to be:-

['London','England','Netherland']

Upvotes: 0

Views: 29

Answers (1)

user7864386
user7864386

Reputation:

IIUC, one option is to use any in a list comprehension to check if a sub-string in sub_list exists in a string in main_list to filter the relevant strings:

out = [place for place in main_list if any(w in place for w in sub_list)]

Output:

['London', 'England', 'Netherland']

Upvotes: 1

Related Questions