List Comprehensions in Python
List comprehensions are a powerful and concise way to create new lists in Python. They combine loops and conditions into a single, readable line.
Instead of writing a multi-line for loop to build a list, you can often do it in one elegant expression. This makes code shorter, faster, and easier to understand.
List comprehensions are very common in Python code – once you learn them, you'll use them everywhere!
Basic List Comprehension
The syntax is: [expression for item in iterable]
Example: Create a list of squares.
With Condition – Filtering
Add if condition to include only certain items: [expression for item in iterable if condition]
Transforming with Expression
The expression at the beginning can be any calculation or function call.
Nested List Comprehensions
For multi-dimensional data, you can nest comprehensions.
When to Use List Comprehensions
Use them for simple transformations and filtering. For complex logic with multiple statements, regular loops are clearer.
Quick Quiz
Hemos revisado y comprobado los materiales, pero aún pueden existir errores. El contenido se ofrece únicamente con fines educativos, así que úsalo bajo tu propia responsabilidad y verifica con otras fuentes si es necesario.
✨ Pregunta a Lara — tu compañera de estudio con IA
Desbloquea soporte de aprendizaje personalizado. Lara puede explicar lecciones, resumir temas y responder tus preguntas — disponible desde el plan Go y superiores.
Lara te ayuda a aprender más rápido — exclusivo para los miembros ReadyTools Go, Plus y Max.


