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 ?

24th Dec 2019, 5:56 AM
❇️MA Azjath ❇️
❇️MA Azjath ❇️ - avatar
5 Answers
+ 2
Do you know how "all" statement works ?
24th Dec 2019, 5:57 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 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.
24th Dec 2019, 6:10 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 2
Aymane Boukrouh Great explanation. 👍
24th Dec 2019, 8:41 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
i understood from your explanation thanks
24th Dec 2019, 6:36 AM
❇️MA Azjath ❇️
❇️MA Azjath ❇️ - avatar
0
that is my problem
24th Dec 2019, 6:02 AM
❇️MA Azjath ❇️
❇️MA Azjath ❇️ - avatar