Python Forum
return next item each time a function is executed
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
return next item each time a function is executed
#12
The code you posted works correctly.
for applicant_name in new_applicants():
    print(f'Here we have applicant {applicant_name}.')
I want to see an example of your code where the generator is not yielding the results you want.

Yoriz is asking if you want your program to remove applicants as they are used by your program. You run your program and it asks for two applicants. The applicants are Mathew and Michael. You run your program again and ask for an applicant. The applicant is Natalie. You run your program again, ask for an applicant and are told there are no remaining applicants.

Is that what you want? If so, you will need an external applicant reference that is modified each time you ask for an applicant. This could be as simple as a file, where you read and remove the first applicant each time your function is called.

That is not what you asked for in your first post. You asked for a function that acts like an iterator. Each time the function is called it returns the next value from the list. You could write such a thing, but why? You can already get this behavior using an iterator, as I demonstrated in my reply. You could bundle this up with some syntactic sugar to make it look like a function call.
job_applicants = {'applicants': {'names': [
{'name': 'Matthew', 'key2': '...', 'key3': 'value1'}, 
{'name': 'Michael', 'key2': '...', 'key3': 'value2'}, 
{'name': 'Natalie', 'key2': '...', 'key3': 'value3'}]}}
 
def applicant_generator():
    for each_applicant in job_applicants['applicants']['names']:
        applicant_name = each_applicant['name']
        yield applicant_name

applicant_iterator = applicant_generator()

def new_applicant():
    return next(applicant_iterator)

print(new_applicant())
print(new_applicant())
print(new_applicant())
This does what you asked, but I think it is a bad idea. Why hide that you are using an iterator?
Reply


Messages In This Thread
RE: return next item each time a function is executed - by deanhystad - Aug-06-2023, 12:17 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  nested function return MHGhonaim 2 728 Oct-02-2023, 09:21 AM
Last Post: deanhystad
  How to not open the context menu if a mouse gesture is executed? MicheliBarcello 2 718 Aug-22-2023, 02:47 PM
Last Post: deanhystad
  code not working when executed from flask app ThomasDC 1 1,032 Jul-18-2023, 07:16 AM
Last Post: ThomasDC
  function return boolean based on GPIO pin reading caslor 2 1,306 Feb-04-2023, 12:30 PM
Last Post: caslor
  return vs. print in nested function example Mark17 4 1,861 Jan-04-2022, 06:02 PM
Last Post: jefsummers
  Remove an item from a list contained in another item in python CompleteNewb 19 5,997 Nov-11-2021, 06:43 AM
Last Post: Gribouillis
  time function does not work tester_V 4 3,180 Oct-17-2021, 05:48 PM
Last Post: tester_V
  How to invoke a function with return statement in list comprehension? maiya 4 2,995 Jul-17-2021, 04:30 PM
Last Post: maiya
  Function - Return multiple values tester_V 10 4,708 Jun-02-2021, 05:34 AM
Last Post: tester_V
  Why recursive function consumes more of processing time than loops? M83Linux 9 4,417 May-20-2021, 01:52 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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