Suppose I had a list
my_list = ['91 9925479326','18002561245','All the best','good']
Now I want to ignore the strings in the list starting with 91
and 18
like below
result = [] for i in my_list: if not '91' in i: if not '18' in i: result.append(i)
So here I want to achieve this with list comprehensions.
Is there anyway to write two if conditions in list compreshensions?
“For loop” is around 50% slower than a list comprehension (65.4/44.5≈1.47).
As we can see, the for loop is slower than the list comprehension (9.9 seconds vs. 8.2 seconds). List comprehensions are faster than for loops to create lists. But, this is because we are creating a list by appending new elements to it at each iteration.
[i for i in my_list if '91' not in i and '18' not in i]
Note you shouldn't use list
as a variable name, it shadows the built-in function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With