pythonlist

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


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?


Solution

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