Using the built-in assert
statement, is there a good, Pythonic way of checking emptiness in an iterable? I have seen:
- One-liner to check whether an iterator yields at least one element?;
- Is there any way to check with Python unittest assert if an iterable is not empty?; and
- Best way to check if a list is empty;
but I'm looking for a solution using assertions. The following looks to work, but I am unsure whether I'm missing some important possible exception:
a = [1, 2, 3]
b = []
assert a, 'a is empty'
assert b, 'b is empty'
Which raises this AssertionError
:
Traceback (most recent call last):
File "<ipython-input-9-185714f6eb3c>", line 5, in <module>
assert b, 'b is empty'
AssertionError: b is empty