Aug-06-2023, 12:17 PM
(This post was last modified: Aug-06-2023, 12:27 PM by deanhystad.)
The code you posted works correctly.
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.
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?