vlad vlad - 3 months ago 8
Python Question

Return True if item is empty string

Please consider this snippet:

>>> i = ["", 1, 2]
>>> all([x for x in i])
False


What would be Pythonic way to make this snippet return
True
even if item in iterable is empty string?

Standard restrictions should apply - if item is
None
or
False
etc, it should return False as expected.

Answer
all([x for x in i if not isinstance(x, str)])

The only falsy string is the empty string, so if you don't want to test for it, then filter out the strings.