Home > Mobile >  Return sub list of elements based on matching sub strings from another list
Return sub list of elements based on matching sub strings from another list

Time:05-07

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']

CodePudding user response:

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']
  • Related