Re: recognizing empty iterators

Discussion in 'Python' started by Bengt Richter, Jul 22, 2003.

  1. On 21 Jul 2003 07:26:15 -0700, (Michele Simionato) wrote:

    >After a recent thread on .endswith, I have been thinking about iterators.
    >I realized that I don't know a satisfactory way to check if an
    >iterator is empty. In other words I am looking for an
    >"isempty" function to use in "if" statements such as
    >
    >if isempty(iterator):
    > do_something()
    >
    >without side effects. Here are some unsatisfactory ways of implementing
    >"isempty":
    >
    >#1: converting to a list or tuple
    >def isempty(iterator):
    > return not list(iterator)
    >
    >Easy, but one has to create the entire list, thus defecting the basic
    >purpose of the iterator, i.e. lazy evaluation.
    >
    >#2: checking for StopIteration
    >def isempty(iterator):
    > try:
    > iterator.next()
    > except StopIteration:
    > return True
    > else:
    > return False
    >
    >This works, for instance
    >
    >print isempty(iter([]))
    >
    >gives True and
    >
    >it=iter([1,2,3])
    >print isempty(it)
    >
    >gives False. However, there is a side effect: after the check, the
    >iterator has advanced of one step and now "it.next()" gives 2, not 1.
    >In order this to work without side effects, I should be able to restart
    >the iterator from the beginning, but I don't know how to do that.
    >Is it possible?
    >
    >#3: comparing with the empty iterator
    >
    >emptyiterator=iter([])
    >
    >it=iter([])
    >
    >if it == emptyiterator: print 'Ok!'
    >
    >This simply doesn't work.
    >
    >I wonder if the itertools module should contain a function to check for
    >empty iterators, thus simplifying my life ;) Of course, I may well be
    >missing something obvious, if so, please enlighten me.
    >

    If iterators had a .peek() method for 1-item lookahead, maybe that would
    be easy to implement. If empty, it could raise StopIteration, and otherwise
    return what next() would return, without disturbing the state w.r.t. next().

    Alternatively, it could return [nextitem] vs [] if at the end
    (without raising StopIteration).

    Regards,
    Bengt Richter
     
    Bengt Richter, Jul 22, 2003
    #1
    1. Advertising

  2. Bengt Richter

    John Roth Guest

    "Bengt Richter" <> wrote in message
    news:bfhvnk$hvd$0@216.39.172.122...
    > On 21 Jul 2003 07:26:15 -0700, (Michele Simionato) wrote:
    >
    > >After a recent thread on .endswith, I have been thinking about iterators.
    > >I realized that I don't know a satisfactory way to check if an
    > >iterator is empty. In other words I am looking for an
    > >"isempty" function to use in "if" statements such as
    > >
    > >if isempty(iterator):
    > > do_something()
    > >
    > >without side effects. Here are some unsatisfactory ways of implementing
    > >"isempty":
    > >
    > >#1: converting to a list or tuple
    > >def isempty(iterator):
    > > return not list(iterator)
    > >
    > >Easy, but one has to create the entire list, thus defecting the basic
    > >purpose of the iterator, i.e. lazy evaluation.
    > >
    > >#2: checking for StopIteration
    > >def isempty(iterator):
    > > try:
    > > iterator.next()
    > > except StopIteration:
    > > return True
    > > else:
    > > return False
    > >
    > >This works, for instance
    > >
    > >print isempty(iter([]))
    > >
    > >gives True and
    > >
    > >it=iter([1,2,3])
    > >print isempty(it)
    > >
    > >gives False. However, there is a side effect: after the check, the
    > >iterator has advanced of one step and now "it.next()" gives 2, not 1.
    > >In order this to work without side effects, I should be able to restart
    > >the iterator from the beginning, but I don't know how to do that.
    > >Is it possible?
    > >
    > >#3: comparing with the empty iterator
    > >
    > >emptyiterator=iter([])
    > >
    > >it=iter([])
    > >
    > >if it == emptyiterator: print 'Ok!'
    > >
    > >This simply doesn't work.
    > >
    > >I wonder if the itertools module should contain a function to check for
    > >empty iterators, thus simplifying my life ;) Of course, I may well be
    > >missing something obvious, if so, please enlighten me.
    > >

    > If iterators had a .peek() method for 1-item lookahead, maybe that would
    > be easy to implement. If empty, it could raise StopIteration, and

    otherwise
    > return what next() would return, without disturbing the state w.r.t.

    next().
    >
    > Alternatively, it could return [nextitem] vs [] if at the end
    > (without raising StopIteration).


    I think that's two separate functions. isEmpty() is not the same
    thing as willBeEmptyOnNextCall().

    That said, there are places where a 1 item lookahead can be quite
    valuable, and lack of a sliding window is the last major thing I'd
    like to see addressed in the for loop.

    John Roth

    >
    > Regards,
    > Bengt Richter
     
    John Roth, Jul 22, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roger Cantillo

    oleDBConnection not recognizing Database

    Roger Cantillo, Aug 28, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    3,090
    Kevin Spencer
    Aug 28, 2003
  2. Marcin Kaliciñski

    Iterators and reverse iterators

    Marcin Kaliciñski, May 8, 2005, in forum: C++
    Replies:
    1
    Views:
    519
    Kai-Uwe Bux
    May 8, 2005
  3. Steven Taschuk

    Re: recognizing empty iterators

    Steven Taschuk, Jul 22, 2003, in forum: Python
    Replies:
    5
    Views:
    306
    Alan Kennedy
    Jul 23, 2003
  4. Roy Smith

    Testing for empty iterators?

    Roy Smith, Jul 3, 2004, in forum: Python
    Replies:
    7
    Views:
    366
    Larry Bates
    Jul 6, 2004
  5. , India
    Replies:
    10
    Views:
    1,104
    James Kanze
    Aug 8, 2009
Loading...

Share This Page