Reputation: 2978
I have these two lists:
my_targets = ["aa1","bb2"]
my_list = ["aa1_rtc","aa1fp","aar1","bb","bb2_11"]
How can I select only those entries from my_list
that contain any of my_targets
. Please notice that aa1_rtc
and aa1fp
contain aa1
, while aar1
should be filtered out.
final_list = [i for i in my_list if i in my_targets]
len(final_list)
Expected result:
final_list =
["aa1_rtc","aa1fp","bb2_11"]
Upvotes: 1
Views: 92
Reputation: 88226
You can use a list comprehension with any
for this:
[i for i in my_list if any(j in i for j in my_targets)]
# ['aa1_rtc', 'aa1fp', 'bb2_11']
Upvotes: 2