The Python all function with a conditional generator expression returns True. What for?

Can someone help me understand why the following Python script returns True?

x = ''
y = all(i == ' ' for i in x)
print(y)

I assume this is due to the fact that it xis an object with zero length, but cannot fully understand.

+4
source share
2 answers

all()always returns Trueif there is no element in the sequence False.

0 elements are created in your loop, therefore it is returned True.

This is documented :

True, ( ).

.

, any() False, True, , any() :

>>> any(True for _ in '')
False
+8

, all:

True, ( ).

+1

All Articles