I have encountered a problem while testing my code in python. I have a list of floats in my code, one of the items is greater then 1. So, the aaa[1]>1
results in True
. When I try the any(aaa)>1
, I get False
. Could you please elaborate on the issue?
Asked
Active
Viewed 46 times
-3
-
3`any(aaa)>1` doesn't do what you think it does. The `any` function is working perfectly fine here. You aren't checking if any aaa is greater than one. You're checking if any aaa is truthy, and then checking if that true/false value is greater than 1. No surprise, True is not greater than 1 – Pranav Hosangadi Oct 18 '20 at 20:36
-
1[Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question) – martineau Oct 18 '20 at 20:38
-
Please provide the expected [MRE](https://stackoverflow.com/help/minimal-reproducible-example). Show where the intermediate results deviate from the ones you expect. We should be able to paste a single block of your code into file, run it, and reproduce your problem. Off-site links and images of text are not acceptable, in keeping with the purpose of this site. – Prune Oct 18 '20 at 20:40
2 Answers
1
The correct syntax is:
any(item > 1.0 for item in aaa)
Documentation links:

codeape
- 97,830
- 24
- 159
- 188
0
The way the any
function works is as follows: it takes a list of boolean values, and returns True if any of those is True, otherwise if all values are False it returns False. So with this in mind, what you want is something like this:
any([x > 1 for x in aaa])

Anis R.
- 6,656
- 2
- 15
- 37