List Comprehension

Python · Lists

Theory

A list comprehension builds a new list in a single expression: [expression for item in iterable if condition]. Python loops over the iterable, skips items where the optional condition is false, evaluates the expression for each remaining item and collects the results. [x*x for x in range(10) if x % 2 == 0] keeps 0, 2, 4, 6 and 8 and squares them, giving [0, 4, 16, 36, 64]. It is equivalent to creating an empty list, looping, testing and appending, just shorter.

Comprehensions are usually a little faster than the equivalent for loop with append(), because the looping happens inside optimised interpreter code. The loop variable stays inside the comprehension and does not overwrite an outside variable. Put if after the for to filter; put a conditional expression in front to transform every item, e.g. ['even' if x % 2 == 0 else 'odd' for x in nums]. Dict and set comprehensions work the same way.

Why we use it

Transforming and filtering a collection is one of the most common tasks in data work: clean every name, keep sales above a target, square numbers. A comprehension expresses the whole intent in one readable line.

When to use it

Use a comprehension when building a new list from another iterable with a simple transform or filter. If the logic needs several statements, try/except or side effects like printing, write a normal for loop instead. For huge data, a generator (x for x in ...) avoids building the whole list in memory.

How it works

  1. Start an empty result list
  2. Take the next x from range(10)
  3. Does x % 2 == 0 hold? Yes: Compute x*x and add it to the result. No: Skip this x.
  4. Any items left in the iterable? Yes: Go back and take the next x. No: Stop looping.
  5. Return the finished list [0, 4, 16, 36, 64]

Syntax

[expression for item in iterable if condition]

Example

squares = [x*x for x in range(10) if x % 2 == 0]

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic