38

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?

Blaszard
  • 30,954
  • 51
  • 153
  • 233
Shiva Krishna Bavandla
  • 25,548
  • 75
  • 193
  • 313

3 Answers3

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

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • 1
    (+1) for a correct answer and for changing `not '91' in i` to `'91' not in i`. – mgilson Jul 31 '12 at 13:19
  • This answer becomes not so concise and clean as soon as you have five of ten instead of two values to check. – Igor Chubin Jul 31 '12 at 13:29
  • Can we do the same with startwith function because sometimes the list consists of the strings starting with 91 and some times starting with 18, what ever it may be the strings starting with 91 or 18 should be ignored from the list.actually the strings here are phone numbers 91 9885564213 and 1800 3236 2365 – Shiva Krishna Bavandla Jul 31 '12 at 13:30
  • Also I must note that this solution is incorrect at all, you must check values with `startswith` not with `in` – Igor Chubin Jul 31 '12 at 13:31
  • 1
    @IgorChubin -- this answer does what the OP requested -- mainly translated the given for loop into a list-comp. – mgilson Jul 31 '12 at 13:34
  • @mgilson: `Now i want to ignore the strings in the list starting with 91 and 18 like below` – Igor Chubin Jul 31 '12 at 13:37
12

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']
>>> 
Igor Chubin
  • 61,765
  • 13
  • 122
  • 144
  • 1
    I prefer: `[i for i in my_list if not any(i.startswith(x) for x in ['91', '18'])]` but I guess it's personal preference – jamylak Jul 31 '12 at 13:35
  • 1
    `.startswith` also accepts a tuple of strings, so it can be shortened to `[i for i in my_list if not i.startswith(('91', '18'))]`. – MRAB Jul 31 '12 at 15:06
  • @MRAB: Thank you for the perfect hint! I updated the answer and added your variant to it. Thank you very much – Igor Chubin Jul 31 '12 at 15:18
3

You can "merge" both conditions:

if ((not '91' in i) and (not '18' in i))
heltonbiker
  • 26,657
  • 28
  • 137
  • 252
  • 6
    Or simplify using [DeMorgan's Law](http://en.wikipedia.org/wiki/De_Morgan%27s_laws): `if not ('91' in i or '18' in i) – Blckknght Jul 31 '12 at 14:42