---
title: "What is the difference between a list comprehension and a generator expression?"  
description: "What is the difference between a list comprehension and a generator expression?"  
author: "Zeynep Kiliç"  
published: 2026-11-15  
updated: 2026-11-15  
canonical: https://www.mindstick.com/forum/163031/what-is-the-difference-between-a-list-comprehension-and-a-generator-expression  
category: "python"  
tags: ["python", "list comprehension", "generator", "memory", "iterators"]  
reading_time: 1 minute  

---

# What is the difference between a list comprehension and a generator expression?

[x*x for x in data] and (x*x for x in data) look almost the same. What actually differs?

## Replies

### Reply by Agnieszka Wozniak

The brackets decide.

- `[x*x for x in data]` is a **list comprehension**. It computes every item immediately and stores the whole list in memory.
- `(x*x for x in data)` is a **generator expression**. It computes nothing yet. It produces one item at a time, only when you ask for the next one, and uses almost no memory.

A generator can be used only once. After you have looped over it, it is exhausted.

Use a list when you need the results more than once, need len(), indexing or slicing, or the data is small. Use a generator when the data is large, or when you only need to loop once, or feed it into something like `sum()`:

```
total = sum(x*x for x in range(10_000_000))
```

That never builds a ten-million-item list.


---

Original Source: https://www.mindstick.com/forum/163031/what-is-the-difference-between-a-list-comprehension-and-a-generator-expression

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
