Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7666761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:55:02+00:00 2026-05-31T14:55:02+00:00

Many functions of a library I use return generator s instead of list s

  • 0

Many functions of a library I use return generators instead of lists when the result is some collection.

Sometimes I just want to check if the result is empty of not, and of course I can’t write something like:

   result = return_generator()
   if result:
      print 'Yes, the generator did generate something!'

Now I come up with a one-liner that solve this problem without consuming the generator:

   result = return_generator()
   if zip("_", result):
      print 'Yes, the generator did generate something!'

I wonder if there are cleaner ways to solve this problem in one line?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T14:55:04+00:00Added an answer on May 31, 2026 at 2:55 pm

    This zip stuff eats the first item yielded, so it is not a good idea as well.

    You can only detect if a generator has an item yielding by getting and keeping it until needed. The following class will help you to do so.

    If needed, it gets an item from the iterator and keeps it.

    If asked for emptyness (if myiterwatch: ...), it tries to get and returns if it could get one.

    If asked for the next item, it will return the retrieved one or a new one.

    class IterWatch(object):
        def __init__(self, it):
            self.iter = iter(it)
            self._pending = []
        @property
        def pending(self):
            try:
                if not self._pending:
                    # will raise StopIteration if exhausted
                    self._pending.append(next(self.iter))
            except StopIteration:
                pass # swallow this
            return self._pending
        def next(self):
            try:
                return self.pending.pop(0)
            except IndexError:
                raise StopIteration
        __next__ = next # for Py3
        def __iter__(self): return self
        def __nonzero__(self):
            # returns True if we have data.
            return not not self.pending
            # or maybe bool(self.pending)
        __bool__ = __nonzero__ # for Py3
    

    This solves the problem in a very generic way. If you have an iterator which you just want to test, you can use

    guard = object()
    result = return_generator()
    if next(result, guard) is not guard:
        print 'Yes, the generator did generate something!'
    

    next(a, b) returns b if iterator a is exhausted. So if it returns the guard in our case, it didn’t generate something, otherwise it did.

    But your zip() approach is perfectly valid as well…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I do not use that many functions but when I do I tend to
We are managing a C++/C# library and we noticed that many classes and functions
I've created a 'Class Library' in C#, which has many functions with summary (XML
I've written a library of functions to make my engineering homework easier, and use
Many programming languages that use IEEE 754 doubles provide a library function to convert
I'm trying to use the NDK toolchain to build some native library I have
I've got a native DLL with some nice functions that can return (and accept)
Many functions in c take pointer to constant strings/chars as parameters eg void foo(const
I've tried many functions already, but I simply can't figure this out. The right
I have a js variable with many functions, and I want to create in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.