Skip to content
Advertisement

How to compare a value to a list of pairs in a list comprehension?

I need to create a list that is n long where each item is a Boolean. The item will only be True when x is in the range of one or more pairs of integers. For example, my current code is as follows:

JavaScript

My issue is that the number of pairs will vary, so only a and b might exist in one instance, but e and f could also exist in another. If I were to structure my coordinate pairs in a list of tuples like so…

JavaScript

…is there a way to still do this as a list comprehension? If not, what would be the most pythonic approach?

Advertisement

Answer

Yes, you can use a generator expression inside any to accomplish this:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement