Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to use two if conditions in list comprehensions in python [duplicate]

Tags:

python

list

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?

like image 857
Shiva Krishna Bavandla Avatar asked Jul 31 '12 13:07

Shiva Krishna Bavandla


People also ask

How much faster are list comprehensions than for loops?

“For loop” is around 50% slower than a list comprehension (65.4/44.5≈1.47).

How much faster are list comprehensions?

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.


1 Answers

[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.

like image 190
Daniel Roseman Avatar answered Sep 20 '22 16:09

Daniel Roseman