# aiter() function in Python

## Introduction

`aiter()` is a new function that came in Python 3.10 version. It returns an [asynchronous iterator](https://docs.python.org/3/glossary.html#term-asynchronous-iterator) for an [asynchronous iterable](https://docs.python.org/3/glossary.html#term-asynchronous-iterable).

The syntax of the `aiter()` function looks like this:

```python
aiter(iter)

```

where `iter` is an [asynchronous iterable](http://docs.python.org/3/glossary.html#term-asynchronous-iterable).

## Examples

When you go look at [this page](https://www.python.org/dev/peps/pep-0530/), one of the first examples you will see looks like this the following:

```python
result = [i async for i in aiter() if i % 2]

```

If you attempt to follow that syntax though, you'll end up with SyntaxError .

```bash
$ py main.py
  File "C:\Users\ashut\Desktop\Test\Blog-Codes\Built-Ins\aiter\main.py", line 1
    result = [i async for i in aiter() if i % 2]
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: asynchronous comprehension outside of an asynchronous function

```

To create a real async comprehension, you will need to call another `async def` function. Let's see an example:

```python
import asyncio

async def numbers(nums):
    for i in range(nums):
        yield i
        await asyncio.sleep(0.5)

async def main():
    odd_nums = [i async for i in aiter(numbers(10)) if i % 2 == 0]
    print(odd_nums)

if __name__ == " __main__":
    event_loop = asyncio.get_event_loop()
    try:
        event_loop.run_until_complete(main())
    finally:
        event_loop.close()

```

When you run this code, you'll get this output:

```bash
[0, 2, 4, 6, 8]

```

In the above code, the numbers() function is an [asynchronous generator](https://docs.python.org/3/glossary.html#term-asynchronous-generator) that is yielding values to our asynchronous list comprehension.

## Conclusion

This was a brief introduction to `aiter()` in Python. You can find more examples [here](https://python.hotexamples.com/examples/coroutils.generator/-/aiter/python-aiter-function-examples.html).
