CBSE 2026 results are out, Mukul scored a perfect 100/100 in Computer ScienceSee all toppers →

KwickAcademy Python · 6 min · free

List Comprehensions

6 min4 KwickClipsFull text belowFree
Next lesson →Kajal Ma'am (MCA), teaching since 2004Remembered in this browser

Learn list comprehensions in Python: build a list in one line, filter with if, use if-else, write nested loops, and know when a loop is clearer.

On screen in this lesson

Syntax, part by part

PartJobExample
expressionthe new valuen * n
for itemnames each valuefor n
in iterablevalues come fromin range(1, 6)
if conditionoptional filterif n > 2

When a comprehension hurts

More than two for parts, or many conditions
Line too long to read in one go
Used only for a side effect, like print
Needs try-except or several steps per item
Rule: if a friend cannot read it, use a loop

Quick recap

Pattern: [expression for item in iterable if condition]
if at the end filters; if-else at the front changes
Nested for parts follow the loop order
Use a loop when the line becomes hard to read

Quick answers

What is a list comprehension?

A short way to build a new list from a loop, in one line.

Where does if go vs if-else?

An if at the end filters values; an if-else at the front changes every value.

KwickClips from this lesson

Short clips, one idea each. Good for revision the night before.

The full lesson, in text

Hello students, welcome to Kwickprep. Can four lines of Python become just one clear line? Yes, when you are building a list. Today we will learn list comprehensions, how to add conditions and nested loops, and when you should not use them.

First, the long way. A list is many values inside square brackets. We start with an empty list called squares. Range of one comma six gives the numbers one to five, because the stop value six is not included. For each number n, append adds n times n to the end of the list. So we get the squares, one, four, nine, sixteen and twenty five.

Now the same list in one line. This is called a list comprehension, which is a short way to build a new list from a loop. Read it like English. Make a list of n times n, for each n in range of one comma six. There is no empty list and no append. The output is exactly the same.

Every list comprehension has the same pattern: expression, for item, in iterable, if condition. The expression is the value that goes into the new list, like n times n. For item gives a name to each value, one at a time. An iterable is anything you can loop over, like a list, a string or a range. The if condition is optional, like if n is greater than two, and it keeps only passing values.

The expression can be anything that gives a value. Here s is each snack name in the canteen list. Upper makes every letter capital, so we get a new list of capital names. Len counts the letters, so we get six, four and four. The original snacks list does not change.

Now let us add a condition. The greater than or equal to sign checks if a value is at least thirty three. Here m is each mark. Only marks that pass the test go into the new list. Pause and predict. Will thirty three be in the list? Yes, because greater than or equal to includes thirty three. Twenty eight and twelve are left out.

What if we want to keep every value, but change it in two ways? Then we put if and else before the for. Here m is the marks list, x is each mark, and r is the result. Each mark becomes P for pass or F for fail. Remember this rule. An if at the end filters values out. An if else at the front changes every value, and removes nothing.

A nested loop is a loop inside another loop. Think of seat numbers in a cinema hall. The outer loop takes each row letter, A and then B. For each row, the inner loop takes the numbers one to three. Str turns a number into text, so row plus str of num joins them, like A one. We get six seat numbers.

Here is the same list as a comprehension. The two for parts are written in the same order as the loops, outer first, then inner. We split it over three lines to read it easily. This is allowed, because Python lets a line continue inside brackets. The output is exactly the same.

Another common use is to flatten a table. A table here is a list of lists, and each small list is a row. For each row in table, and for each x in that row, keep x. So all six numbers come out in one flat list. The table has three rows, and the flat list has six values.

A comprehension is not always better. If it has more than two for parts or many conditions, it becomes a puzzle. If the line is too long to read in one go, split it or use a loop. Never use it only for a side effect, which means doing something like printing instead of building a list. If each item needs several steps or error handling, a normal loop is clearer. A simple rule is, if a classmate cannot read it quickly, write a loop.

Here is a bad example. Someone used a comprehension just to print each number. The numbers print, but Python also builds a useless list. The print function gives back None, which means no value. So junk is a list of two Nones. A simple for loop would have been the right choice here.

Let us revise what we learned today. A list comprehension follows the pattern expression, for item, in iterable, if condition. An if at the end filters values, while an if else at the front changes every value. Nested for parts are written in the same order as normal loops. And when a comprehension becomes hard to read, a normal loop is the better choice. Try turning three of your old loops into comprehensions.

Courses that teach this

CourseUnit
Programming All levels PythonData Structures

Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.

Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

Want a plan that actually fits your board dates?

Ask Kajal Ma'am directly, 20+ years teaching computer science. Free demo class first, no payment.

Talk to Kajal Ma'am on WhatsApp

Or see the Class 12 Computer Science course →

Studying outside India?

We coach CBSE, IGCSE & international students across the globe, one-to-one, in your local time zone.

Visit International →