Python Forum

Full Version: return next item each time a function is executed
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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!
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
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?
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')
        
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?
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
Thank you. Again it does not work. Confused

I am using Python 3.8.3.

I am confused.
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.
(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.
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.
Pages: 1 2