0

I've been analyzing solutions to one problem on the Internet and stumbled upon this.

a=int(input())
array = []
for i in range (a):
    b=float(input())
    array+=[round(b)]
print(min(array))
if any(c>10 for c in array):
    print("YES")
else:
    print("NO")

I get the idea of how this code works, but can't figure out what exactly this line does.

if any(c>10 for c in array):

My basic understanding is that it checks if at least one element in the array is greater than 10. But I don't really get how it works at it's core. Like, why do you have to introduce a new variable in there? Or how does it bind this variable to the array?

  • You’re looking at a *generator expression*; looking up *list comprehensions* also puts you on the right track. – deceze Apr 29 '22 at 16:54
  • https://peps.python.org/pep-0289/ – Nin17 Apr 29 '22 at 16:54
  • `c>10 for c in array` is a generator expression that yields booleans (`True`, `False`). For every element `c` in the collection `array`, the generator will yield `True` if the condition `c > 10` is satisfied, `False` otherwise. `any` will consume items from the generator expression until it encounters a truthy item, at which point it will return `True`. If `any` manages to completely exhaust the generator without encountering a truthy item, it returns `False`. – Paul M. Apr 29 '22 at 16:55
  • You are likely confused because `any` is no ordinary variable in this case: it is an existing Python built-in function (just like `print`) – jsbueno Apr 29 '22 at 16:56
  • @jsbueno I think OP was actually confused about why you have to intoduce ```c``` – Nin17 Apr 29 '22 at 17:35
  • @Nin17 Yeah. Now trying to understand the concept of a generator. – imstuckwithsumfindumbsoplshelp Apr 29 '22 at 17:44

0 Answers0