Python Forum
testing if an iterator is empty
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
testing if an iterator is empty
#1
is there a way to test if an iterator is empty without "consuming" any content in case it is not empty? does a true/false test accomplish this? i don't want to iterate them to a list because some may be too large or infinite.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
When an iterator is empty it should return aStopIteration
Reply
#3
You can use more_itertools.peekable() to simulate this. Testing truth value on a peekable tells whether it is empty
>>> import more_itertools as mit
>>> import io
>>> f = io.StringIO("""\
... Hello
... World
... !!!""")
>>> z = mit.peekable(f)
>>> z.peek()
'Hello\n'
>>> z.peek()
'Hello\n'
>>> bool(z)
True  # z is not exhausted
>>> for line in z:
...     print(line)
... 
Hello

World

!!!
>>> bool(z) # z is exhausted
False
However, internally, the first time you call peek(), the inner iterator f is advanced. It is impossible to determine if a general iterator is empty without trying to advance it.
Reply
#4
(Feb-23-2022, 12:59 AM)BashBedlam Wrote: When an iterator is empty it should return aStopIteration

what happens if it is not empty?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
so the short answer to "is there a way ... ?" is "no".
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
Would it help to roll you own iterator?
class My_Personal_Iterator :
	def __init__ (self, maximum) :
		self.maximum = maximum
		self.output_number = 0

	def __iter__ (self) :
		return self

	def check_iterator (self) :
		return self.maximum - self.output_number

	def __next__ (self) :
		self.output_number += 1
		if self.output_number <= self.maximum :
			return self.output_number
		else :
			return StopIteration

tester = My_Personal_Iterator (7)
print (next (tester))
print (next (tester))
print (next (tester))
iterations_left = tester.check_iterator ()
print (f'There are {iterations_left} iterations left.')
print (next (tester))
print (next (tester))
print (next (tester))
print (next (tester))
iterations_left = tester.check_iterator ()
print (f'There are {iterations_left} iterations left.')
print (next (tester))
Output:
1 2 3 There are 4 iterations left. 4 5 6 7 There are 0 iterations left. <class 'StopIteration'>
Reply
#7
(Feb-24-2022, 09:21 PM)BashBedlam Wrote: Would it help to roll you own iterator?

no. this needs to work with whatever iterator it gets.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
(Feb-24-2022, 08:23 PM)Skaperen Wrote: so the short answer to "is there a way ... ?" is "no".
Reply
#9
Skaperen Wrote:this needs to work with whatever iterator it gets.
You want something that is structurally impossible. Consider the following iterator
def user_inputs():
    while True:
        yield input("Give me a string: ")

iterator = user_inputs()
Now 'iterator' is empty iff the user decides not to type the Enter key until the program exits. There is no way for the program to read the user's mind. Even if the program calls next(iterator), the answer will be known only if this call returns before the program exits.
Reply
#10
the code i was going to use this for gets an object passed via a method argument. it tests to see if it is an iterator using isinstance(the_arg,collections.abc.Iterator) then it would be nice to know if it is empty. but this is not essential to know. it could just handle things better, i'll still do the if not test in case i's an empty sequence.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  multiplying an iterator Skaperen 9 3,339 Oct-12-2021, 07:07 PM
Last Post: Skaperen
  iterating an iterator by one step Skaperen 0 1,599 Feb-09-2021, 08:38 PM
Last Post: Skaperen
  modifying the origin of an iterator Skaperen 4 2,990 Mar-05-2020, 11:49 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020