title: Most elegant way to branch if a generator is empty in Python
body: <p>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</p>
  
  <pre><code>if generator is empty:
      perform some action
  else:
      for item in generator:
          perform some actions
  </code></pre>
  
  <p>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 <a href="https://stackoverflow.com/a/664239/161801">https://stackoverflow.com/a/664239/161801</a>, which seems very inelegant, I guess because it has to treat the first element of the generator separately from the rest. </p>