vlad vlad - 1 year ago 105
Python Question

Return True if item is empty string

Please consider this snippet:

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

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

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

Answer Source
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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download