'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']
Solution 1:[1]
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']
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
