import asyncio import string class CharIterator: def __init__(self): self.pos = 0 def __aiter__(self): return self async def __anext__(self): while self.pos < len(string.ascii_lowercase): self.pos += 1 char = string.ascii_lowercase[self.pos - 1] if self.pos == 7: return char.capitalize() elif self.pos in (21, 18, 20, 1, 13): return char def char_pos_gen(): char_pos = [4, 0, 5, 2, 3, 1] for pos in char_pos: yield pos async def main(): dw = {} cpg = char_pos_gen() async for char in CharIterator(): try: dw[next(cpg)] = char except StopIteration: break res = '' for n in sorted(dw): res += dw[n] print(res) if __name__ == '__main__': loop = asyncio.get_event_loop() try: loop.run_until_complete(main()) finally: loop.close()