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
#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


Messages In This Thread
testing if an iterator is empty - by Skaperen - Feb-23-2022, 12:23 AM
RE: testing if an iterator is empty - by BashBedlam - Feb-23-2022, 12:59 AM
RE: testing if an iterator is empty - by Skaperen - Feb-24-2022, 08:20 PM
RE: testing if an iterator is empty - by Skaperen - Feb-24-2022, 08:23 PM
RE: testing if an iterator is empty - by BashBedlam - Feb-24-2022, 09:21 PM
RE: testing if an iterator is empty - by Skaperen - Feb-24-2022, 10:35 PM
RE: testing if an iterator is empty - by BashBedlam - Feb-24-2022, 10:40 PM
RE: testing if an iterator is empty - by Gribouillis - Feb-24-2022, 11:38 PM
RE: testing if an iterator is empty - by Skaperen - Feb-25-2022, 02:31 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  multiplying an iterator Skaperen 9 3,412 Oct-12-2021, 07:07 PM
Last Post: Skaperen
  iterating an iterator by one step Skaperen 0 1,627 Feb-09-2021, 08:38 PM
Last Post: Skaperen
  modifying the origin of an iterator Skaperen 4 3,040 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