To determine a unified value with a Python for statement

Asked 2 years ago, Updated 2 years ago, 20 views

fori in a: #a>> (True, False, True)
    If not neighbor.get_visited(): #Code that stops if multiple a has a value that is false
        break

Code to be executed once (
...
)

I want to check all the values in a and let the code run once below all true, but if I make an ellse statement, the for statement keeps running while it repeats, and when I take it out, it just runs regardless of the if statement.

Is there a way to check the value in a and proceed with the code below only when they are all the same?

ps. Even if it is not a for statement, it is also good to check the value in the list and execute the code below when it is the same.

python

2022-09-20 16:23

1 Answers

Python has all, any.

if all(lst):
  print(lst, "is all true")

if any(lst):
  print (lst, "at least one of them is true")


2022-09-20 16:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.