Use the all() function to check if the True condition is True for all elements:
all(el == 'hello' for el in x)
The all() function takes an iterable (something that gives results one by one) and will only return True if all of these elements are true. When he finds something false, he will return False and will not look any further.
Here, iterable is a generator expression that performs an equality test for each element in the input sequence. The fact that all() stops repeating early if a false value is encountered makes this test very effective if the test in the expressed expression of the False generator for any element is at an early stage.
Note that if x empty, then all() returns True , and also will not find any elements that are false in the empty sequence. You can verify that the first sequence is nonempty:
if x and all(el == 'hello' for el in x):
to get around this.
source share