Aug-05-2023, 09:33 PM
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:
>>> 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!
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!
