0
List function in python
a=[1,2,3,4,5] if all([i>5 for i in a]): print(“something “) Can anyone explain to me how it’s act ?
5 odpowiedzi
+ 2
Do you know how "all" statement works ?
+ 3
MA Azjath 💯❇️🌐❇️ okay, then let start with it first (even tho I believe it was explained in the course, or at least has been asked before):
Let say you have a list, containing boolean values. If ALL the values are True, then the all(list) will return True, example:
list = [True, True, True, True]
all(True)
If ANY of the values is False, the all(list) will return False, example:
list = [True, True, False, True]
all(list)
Now back to you problem, the [i>5 for i in a] will return this list:
[False, False, False, False, False] (because none of the values is strictly superior to 5)
Since at least one value is False, then the all statement, by definition, will return False, thus the if statement is False, and nothing will be printed.
+ 2
Aymane Boukrouh Great explanation. 👍
+ 1
i understood from your explanation thanks
0
that is my problem