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


This question already has an answer here:


[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 have more than two values (91 and 18) or they are dynamically produced it is better to use this construction:

[i for i in my_list if not i.startswith(('91', '18'))]

Or if you want to check if 91 and 18 are in the strings (not only in the beginning), use in instead of startswith:

[i for i in my_list if all(x not in i for x in ['91', '18'])]

Example of usage:

>>> my_list = ['91 9925479326','18002561245','All the best','good']
>>> [i for i in my_list if all(not i.startswith(x) for x in ['91', '18'])]
['All the best', 'good']

You can "merge" both conditions:

if ((not '91' in i) and (not '18' in i))

Need Your Help

What is the correct syntax of ng-include?

javascript html5 include angularjs repeater

I’m trying to include an HTML snippet inside of an ng-repeat, but I can’t get the include to work. It seems the current syntax of ng-include is different than what it was previously: I see many exa...

Unable to execute dex: Multiple dex files define

java android

I know this question has been asked here a few times before. But i haven't seen any possible solution yet.