combine if filter terms from list

Ben Finney ben+python at
Fri Apr 21 21:18:28 EDT 2017

Rory Schramm <etfeet at> writes:

> I'm trying to use python list comprehensions to combine multiple terms
> for use by a for loop if condition.

Thank you for a small code example. It doesn't have enough to illustrate
the problem you're describing; we can't run it and see what you're seeing.

> filters = [ 'one', 'two', 'three']
> for line in other_list:
>     if ' and '.join([item for item in filters]) not in line[2]:
>         print line

What is ‘line’?

What is the example input, and what output are you expecting to see, and
what output do you see instead?

Please construct and present a small and also *complete* example, that
we can also run to have a chance of seeing the same behaviour.

> The problem I'm having is the for loop isn't filtering out the terms from
> the filter list. I suspect the problem is the if condition is treating the
> results for the list comprehension as a literal string and not part of the
> if condition itself. I'm not sure how to fix this though.

Without a complete exampel, and a comparison between what the actual
output is versus what you expect to see, I am not able to understand
the problem description, especially “not part of the if condition itself”.

> Any ideas on How to make this work?

Once we can see a complete small example that demonstrates the
behaviour, we may have a better chance.

 \      “If you don't fail at least 90 percent of the time, you're not |
  `\                                    aiming high enough.” —Alan Kay |
_o__)                                                                  |
Ben Finney

More information about the Python-list mailing list