I am trying to write a method that returns a Generator
. The end result of these two methods is to get a combination of two lists in the form: 'A #1', 'B #1', ..., 'F #9'
FLATS = ['A', 'B', 'C', 'D', 'E', 'F']
def generate_nums() -> Generator[str, None, None]:
prefix = '#'
for num in range(10):
code = ''.join([prefix, str(num)])
yield code
def generate_room_numbers() -> Generator[str, None, None]:
room_nums = generate_nums()
yield (' '.join([flat_name, room_num]) for room_num in room_nums for flat_name in FLATS)
if __name__ == "__main__":
result = generate_room_numbers()
result = next(result) # I hate this. How do I get rid of this?
for room in result:
print(room)
This gives me the correct outcome. Although, my annoyance is the line result = next(result)
. Is there a better way to do this? I looked at this answer as well as the yield from
syntax but I can barely understand generators enough as it is.