> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Python Async Comprehension

Asynchronous comprehensions extend standard Python comprehensions by allowing iteration over asynchronous iterables using `async for`, and the evaluation of awaitables using the `await` keyword, within list, set, dictionary comprehensions, and generator expressions. Introduced in PEP 530, they enable concise syntax for consuming and transforming asynchronous data streams.

## Syntax Variations

Asynchronous comprehensions manifest in two primary forms, which can also be combined:

**1. Asynchronous Iteration (`async for`)**
Used when the source data is an asynchronous iterable (an object implementing `__aiter__` and `__anext__`).

```python theme={"dark"}
[expression async for item in async_iterable if condition]
```

**2. Awaiting Expressions (`await`)**
Used when the transformation expression yields an awaitable (like a coroutine or Task), even if the underlying iterable is synchronous.

```python theme={"dark"}
[await expression for item in iterable if condition]
```

**3. Combined**
Used when both the iterable is asynchronous and the transformation requires awaiting.

```python theme={"dark"}
[await expression async for item in async_iterable if condition]
```

## Supported Data Structures

The asynchronous syntax applies to all native comprehension types:

* **List Comprehension:** `[x async for x in async_iterable]`
* **Set Comprehension:** `{x async for x in async_iterable}`
* **Dict Comprehension:** `{k: v async for k, v in async_iterable}`
* **Generator Expression:** `(x async for x in async_iterable)`

*Note: An asynchronous generator expression returns an asynchronous iterator, not a standard generator. It must be consumed using `async for` or `anext()`.*

## Execution Rules and Constraints

1. **Context Requirement:** Asynchronous comprehensions and generator expressions containing `async for` or `await` can only be defined and executed within an `async def` function or an asynchronous context.
2. **Evaluation:** While the comprehension builds the collection synchronously in memory, it yields control back to the event loop at each `async for` step or `await` expression.
3. **Sequential Execution:** The iterations and awaits inside the comprehension are evaluated sequentially, not concurrently. `[await coro(x) for x in iterable]` will await each coroutine one after the other. To execute them concurrently, `asyncio.gather()` must be used instead of a comprehension.

## Mechanical Demonstration

The following code illustrates the mechanics of asynchronous comprehensions without relying on external I/O libraries.

```python theme={"dark"}
import asyncio


# Asynchronous iterable (Async Generator)
async def async_number_generator():
    for i in range(3):
        yield i


# Awaitable (Coroutine)
async def async_transform(x):
    return x * 10

async def main():
    # 1. Consuming an async iterable into a list
    list_comp = [x async for x in async_number_generator()]
    # Result: [0, 1, 2]

    # 2. Awaiting a coroutine within a synchronous iteration
    await_comp = [await async_transform(x) for x in range(3)]
    # Result: [0, 10, 20]

    # 3. Combined: Consuming an async iterable and awaiting a coroutine
    combined_comp = [await async_transform(x) async for x in async_number_generator()]
    # Result: [0, 10, 20]

    # 4. Asynchronous Dictionary Comprehension
    dict_comp = {x: await async_transform(x) async for x in async_number_generator()}
    # Result: {0: 0, 1: 10, 2: 20}

    # 5. Asynchronous Generator Expression
    async_gen_expr = (x ** 2 async for x in async_number_generator())
    
    # Consuming the resulting asynchronous iterator
    gen_results = [x async for x in async_gen_expr]
    # Result: [0, 1, 4]

asyncio.run(main())
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Python Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
