Python Forum
return next item each time a function is executed - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: return next item each time a function is executed (/thread-40484.html)

Pages: 1 2


return next item each time a function is executed - User3000 - Aug-05-2023

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!


RE: return next item each time a function is executed - snippsat - Aug-05-2023

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



RE: return next item each time a function is executed - User3000 - Aug-05-2023

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?


RE: return next item each time a function is executed - menator01 - Aug-05-2023

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')
        



RE: return next item each time a function is executed - User3000 - Aug-05-2023

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?


RE: return next item each time a function is executed - deanhystad - Aug-05-2023

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



RE: return next item each time a function is executed - User3000 - Aug-06-2023

Thank you. Again it does not work. Confused

I am using Python 3.8.3.

I am confused.


RE: return next item each time a function is executed - Yoriz - Aug-06-2023

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.


RE: return next item each time a function is executed - User3000 - Aug-06-2023

(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.


RE: return next item each time a function is executed - deanhystad - Aug-06-2023

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.