¿Hay alguna manera de usar dos condiciones if en la lista de comprensiones en python [duplicar]?

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 y 18 como abajo

result = []
for i in my_list:
   if not '91' in i:
      if not '18' in i:

So here I want to achieve this with list comprehensions.

Is there anyway to write two if conditions in list compreshensions?

preguntado el 31 de julio de 12 a las 14:07

@jamylak: I had taken that as an example anyway thanks for editing. -

Beware that your tests just check if 91 y 18 están presentes en cualquier position of the string. You should use str.startswith en lugar: docs.python.org/library/stdtypes.html#str.startswith -

3 Respuestas

[i for i in my_list if '91' not in i and '18' not in i]

Tenga en cuenta que no debe usar list as a variable name, it shadows the built-in function.

Respondido 31 Jul 12, 14:07

(+1) for a correct answer and for changing not '91' in i a '91' not in i. - mgilson

This answer becomes not so concise and clean as soon as you have five of ten instead of two values to check. - Igor Chubin

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

Also I must note that this solution is incorrect at all, you must check values with startswith no con in - Igor Chubin

@IgorChubin -- this answer does what the OP requested -- mainly translated the given for loop into a list-comp. - mgilson

You can "merge" both conditions:

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

Respondido 31 Jul 12, 14:07

Or simplify using Ley de DeMorgan: `if not ('91' in i or '18' in i) - Caballero negro

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 y 18 are in the strings (not only in the beginning), use in en lugar de startswith:

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

Ejemplo de uso:

>>> 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']

Respondido 31 Jul 12, 16:07

Yo prefiero: [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

.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

@MRAB: Thank you for the perfect hint! I updated the answer and added your variant to it. Thank you very much - Igor Chubin

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.