The title says it all. The enumerate built-in accepts regular iterables only. I’d like to suggest that it should accept asynchronous iterables as well in order to ensure the same usage pattern in loops:
for i, value in enumerate(x): ... # typical usage
async for i, value in enumerate(ax): ... # proposal
If it is not possible for some reasons, please consider to add an async enumerate counter-part to the Python.
That would require double iteration: one in async_enumerate and another in your async for loop.
import asyncio
async def async_enumerate(aiterable, start=0):
index = start
async for value in aiterable:
yield index, value
index += 1
# An example of an asynchronous iterable (async generator)
async def async_gen():
for i in range(5):
await asyncio.sleep(.5) # Simulate async operation
yield i
async def main():
async for i, value in async_enumerate(async_gen()):
print(f"Index: {i}, Value: {value}")
# Run the main function
asyncio.run(main())