I have a generator in Python, and I want to loop over it if it has items, and perform a different action if it is empty. Something like
if generator is empty:
perform some action
else:
for item in generator:
perform some actions
I know there is no way to tell if a generator is empty without iterating through it, but it seems that there should still be some nice elegant way to perform this logic. The best I can think of is something along the lines of https://stackoverflow.com/a/664239/161801, which seems very inelegant, I guess because it has to treat the first element of the generator separately from the rest.