Check out filter and any in python docs.
>>> data_list = ['a.1','b.2','c.3'] >>> test_list = ['a.','c.'] >>> new_list = filter(lambda x: any(x.startswith(t) for t in test_list), data_list) >>> new_list ['a.1', 'c.3']
Then you can do whatever you want with the new_list .
As @Chepner points out, you can also put a tuple of strings on startswith , so you can also write above:
>>> data_list = ['a.1','b.2','c.3'] >>> test_tuple = ('a.','c.') >>> new_list = filter(lambda x: x.startswith(test_tuple), data_list) >>> new_list ['a.1', 'c.3']
source share