li is some list, and I want to filter a list -- comparable to np.where(...) for a pure python list
In [1]:
li = ['a', 'b', 'c', 'a', 'b', 'a']
print(li)
using filter and lambda¶
In [8]:
t_m1 = %timeit -o list(filter(lambda x:x=='a', li))
In [11]:
filtered_list = list(filter(lambda x:x=='a', li))
print(filtered_list)
using a list comprehension¶
In [12]:
t_m2 = %timeit -o [x for x in li if x == 'a']
In [13]:
list_comp = [x for x in li if x == 'a']
print(list_comp)
comparing results of %timeit¶
In [15]:
t_m1.average
Out[15]:
In [16]:
t_m2.average
Out[16]:
In [18]:
t_m1.average / t_m2.average
Out[18]: