programmerwiz32
programmerwiz32

Reputation: 559

how to use all to filter list above threshold

x=[255, 255, 255, 250, 250, 250, 250, 250, 250, 255, 255, 255]
y=all([i for i in x if i>260])
y1 = all([i for i in x if i>220])
y2 = all([True for i in x if i>260])
y3 = all([True for i in x if i>220])
y1==y2==y3==y #gives True 

How does all handle empty lists, and how can I use to filter items above 220. the only thing working for me now is

y=len([i for i in x if i>220])== len(x) 

what I want to know if all list is above certain number i.e 220

Upvotes: 0

Views: 1875

Answers (3)

m_____z
m_____z

Reputation: 1591

There are some small tweaks required to make your list comprehensions work. For instance, you don't handle the case in which the value in the list is not greater than x. For example, let's assume our list is:

>>> x = [1, 2, 3, 4, 5, 6]

If we use the code snipped from your question:

>>> [True for i in x if i > 3]
[True, True, True]

The result is probably not exactly what you expected. Therefore, calling all() on that list will always evaluate to True. One small tweak, however (note the addition of the else statement):

>>> [True if i > 3 else False for i in x]
[False, False, False, True, True, True]

This is the desired output. Now you can also apply all() to the output list.

>>> y1 = [True if i > 3 else False for i in x]
>>> all(y1)
False

And if we want to know whether all values are greater than 0, we expect all() to return True in the following case:

>>> y2 = [True if i > 0 else False for i in x]
>>> all(y2)
True

To answer your question about how all() handles empty lists:

>>> all([])
True

Depending on the structure of your code and the desired output you may want to have an edge case statement that handles empty lists. Hopefully this is enough input for you to adapt it to your problem statement.


Making the code more compact: As very kindly pointed out in the comments, the code above is a bit "wordy" to make a point and for the sake of the explanation. When writing list comprehensions and condition statements in Python (and perhaps other programming languages), you can actually shorten the list comprehension and condition statement:

>>> y1 = [i > 3 for i in x]
>>> y1
[False, False, False, True, True, True]
>>> all(y1)
False

i > 3 in the list comprehension will turn into True or False and therefore yield the same result as writing True if i > 3 else False.


Finally, if you want to know if all elements meet the threshold and there is at least one element in the list:

>>> y1 = [i > 3 for i in x]
>>> y1
[False, False, False, True, True, True]
>>> all(y1) and y1
[]  # this will evaluate to False if you use it in a condition statement

Alternatively you can also convert it to a boolean:

>>> bool(all(y1) and y1)
False

Which does the right thing for empty lists as well:

>>> bool(all([]) and [])
False

Upvotes: 2

Robin Zigmond
Robin Zigmond

Reputation: 18249

You want this:

all([i > 220 for i in x])

all for an empty list should be True, because all of its elements - all 0 of them - are truthy

Edit: if it's desired to rule out the trivial case where x is empty, then as suggested in the comments just add an additional check for that, such as len(X) > 0

Upvotes: 3

Kevin Wang
Kevin Wang

Reputation: 2729

instead of True for i in x if i>260, do i>260 for i in x

i>260 will result in a boolean: True, or False for each element.

Upvotes: 1

Related Questions