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
#1
hi everyone,

When I loop through my dictionary "job_applicants", it prints out every name at once.

However, I want one name printed each time I run the function. So it should only print out the first applicant 'Matthew'. Next time I run the function 'Michael'. Then 'Natalie' etc ...

This is my code:
job_applicants = {'applicants': {'names': [
{'name': 'Matthew', 'key2': '...', 'key3': 'value1'}, 
{'name': 'Michael', 'key2': '...', 'key3': 'value2'}, 
{'name': 'Natalie', 'key2': '...', 'key3': 'value3'}]}}

def new_applicants():
    for each_applicant in job_applicants['applicants']['names']:
        applicant_name = each_applicant['name']
        yield applicant_name

for applicant_name in new_applicants():
    print(f'Here we have applicant {applicant_name}.')
Output is:
>>> Here we have applicant Matthew.
>>> Here we have applicant Michael.
>>> Here we have applicant Natalie.

However, I would like the following output:

I run the function for the first time and the output should ONLY be:

>>> Here we have applicant Matthew.

I run the function again and the output should ONLY be:

>>> Here we have applicant Michael.

and so on ...

Can someone help?
Very much appreciated!
Reply
#2
job_applicants = {
    'applicants': {
        'names': [
            {'name': 'Matthew', 'key2': '...', 'key3': 'value1'},
            {'name': 'Michael', 'key2': '...', 'key3': 'value2'},
            {'name': 'Natalie', 'key2': '...', 'key3': 'value3'},
        ]
    }
}

def new_applicants():
    for each_applicant in job_applicants['applicants']['names']:
        applicant_name = each_applicant['name']
        yield applicant_name

applicant_generator = new_applicants()
print(f'Here we have applicant {next(applicant_generator)}.')
Output:
Here we have applicant Matthew.
So now if run print again it will call function an get next name.
>>> print(f'Here we have applicant {next(applicant_generator)}.')
Here we have applicant Michael.
>>> print(f'Here we have applicant {next(applicant_generator)}.')
Here we have applicant Natalie.
>>> print(f'Here we have applicant {next(applicant_generator)}.')
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
StopIteration
User3000 likes this post
Reply
#3
hi thank you very much for your reply and solution! Highly appreciated!

However, I wasn't able to print out a different name. I only get Matthew each time I run the function.

Do I need to call the function from a different script or why does it not work?
Reply
#4
Maybe this will help

job_applicants = {
    'applicants': {
        'names': [
            {'name': 'Matthew', 'key2': '...', 'key3': 'value1'},
            {'name': 'Michael', 'key2': '...', 'key3': 'value2'},
            {'name': 'Natalie', 'key2': '...', 'key3': 'value3'},
        ]
    }
}
 
def new_applicants():
    for each_applicant in job_applicants['applicants']['names']:
        applicant_name = each_applicant['name']
        yield applicant_name
 
applicant_generator = new_applicants()
counter = len(job_applicants['applicants']['names'])

while counter > 0:
    print(f'Here we have applicant {next(applicant_generator)}.')
    counter -= 1
    if counter > 0:
        input('Press enter for next applicant')
        
User3000 likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#5
hi thank you for the suggestion! However, in my use case I can't use a user input.

The script should run and return the next name automatically once I call the function again.

But maybe it's simply impossible?
Reply
#6
job_applicants = {'applicants': {'names': [
{'name': 'Matthew', 'key2': '...', 'key3': 'value1'}, 
{'name': 'Michael', 'key2': '...', 'key3': 'value2'}, 
{'name': 'Natalie', 'key2': '...', 'key3': 'value3'}]}}
 
def new_applicants():
    for each_applicant in job_applicants['applicants']['names']:
        applicant_name = each_applicant['name']
        yield applicant_name
 
applicant = new_applicants()

print(next(applicant))
print(next(applicant))
print(next(applicant))
Output:
Matthew Michael Natalie
User3000 likes this post
Reply
#7
Thank you. Again it does not work. Confused

I am using Python 3.8.3.

I am confused.
Reply
#8
Maybe you are asking to get the next value each time the code is run not the function is called.
Each time the code is run it will return to its beginning state.
To alter the state of which item is selected you would need to store that state to be recalled each time the code is run.
User3000 likes this post
Reply
#9
(Aug-06-2023, 08:35 AM)Yoriz Wrote: Maybe you are asking to get the next value each time the code is run not the function is called.
Each time the code is run it will return to its beginning state.
To alter the state of which item is selected you would need to store that state to be recalled each time the code is run.

Yes exactly. I'm sorry for the confusion.

How would I do that? Can you post an example?

Thank you very much.
Reply
#10
Pretty sure you are "doing it wrong". Please post you code so we can see what version of "doing it wrong" you have chosen instead of making us guess. May turn out that we "told you wrong", but we will never know without seeing the code that does't work.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  nested function return MHGhonaim 2 624 Oct-02-2023, 09:21 AM
Last Post: deanhystad
  How to not open the context menu if a mouse gesture is executed? MicheliBarcello 2 675 Aug-22-2023, 02:47 PM
Last Post: deanhystad
  code not working when executed from flask app ThomasDC 1 904 Jul-18-2023, 07:16 AM
Last Post: ThomasDC
  function return boolean based on GPIO pin reading caslor 2 1,193 Feb-04-2023, 12:30 PM
Last Post: caslor
  return vs. print in nested function example Mark17 4 1,757 Jan-04-2022, 06:02 PM
Last Post: jefsummers
  Remove an item from a list contained in another item in python CompleteNewb 19 5,789 Nov-11-2021, 06:43 AM
Last Post: Gribouillis
  time function does not work tester_V 4 3,052 Oct-17-2021, 05:48 PM
Last Post: tester_V
  How to invoke a function with return statement in list comprehension? maiya 4 2,867 Jul-17-2021, 04:30 PM
Last Post: maiya
  Function - Return multiple values tester_V 10 4,470 Jun-02-2021, 05:34 AM
Last Post: tester_V
  Why recursive function consumes more of processing time than loops? M83Linux 9 4,278 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