0Pricing
Python Academy · Lesson

List Comprehensions

Create lists concisely using comprehension syntax with conditions.

Introduction

List comprehensions create new lists from iterables in a single readable expression, replacing common for-loop patterns.

Basic Syntax

[expression for item in iterable] is the core form. [x*2 for x in range(5)] gives [0,2,4,6,8].
doubled = [x*2 for x in range(5)]
print(doubled)

All lessons in this course

  1. List Comprehensions
  2. Set and Dict Comprehensions
  3. Generator Expressions
  4. The yield Keyword
← Back to Python Academy