Skip to contentSkip to navigation

List Comprehensions

Build new lists, dictionaries and sets in one readable line: transform every item, keep only the ones you want, and know when a plain loop is clearer.
  • Module 7, lesson 2
  • About 8 minutes
  • Builds on earlier lessons
  • 12-question quiz at the end

What you will learn

  • From loop to one line
  • Filter with if
  • Pick a value with if/else
  • Watch two lists get built

One line, one new list

A comprehension builds a new list from an old one, in one readable line.

Reading a comprehension

This line squares the even numbers. Tap each part to see its job.

Build lists in one line

Pick an example, change it, then press Run. Say what you expect to see before you press.

Watch two lists get built

Press Step to run one line at a time. Watch the names box change.

Quick guesses

Guess first, then read why. A wrong guess is where the learning happens. Nothing here is scored.

Common comprehension misconceptions

Each card is something learners often believe. Tap it to see what Python really does.

Notes

Everything above, in words. Open it when you want the full explanation.

Syntax

The general shape of the statements in this lesson.

Worked examples

Read the problem, run the code, then read why it works.

Common pitfalls

Mistakes people make with this topic, and what to write instead.

In the real world

Where this shows up in software people use every day.

Key takeaways

Common questions

Guess the answer first, then open it.

Your notes

Sign in to keep private notes alongside this lesson.

Sign in to take notes

Where to go next

The quiz and the Lab live on their own pages, so this lesson stays about learning.

Finished this lesson?

Create an ID or sign in to save your progress across devices.

Create a free ID
Lessons done in this module0/3 complete

Quiz

Check what you learned

12 questions, with an explanation after every answer. A finished attempt is marked on the server and earns XP.

Lab

Keep going in the Lab

7 tasks on this lesson, from writing code to tracing it by hand. Practice only, no XP.

Review

Spaced review

Questions you miss in a quiz come back on a spaced schedule, so what you learned here sticks.

This lesson at a glance

The 6 lines of code this lesson uses, each with a short note. Keep it open while you do the quiz and the Lab.

What else you can search for and explore

Curious to go further? Each one opens a web search in a new tab.

References

Look things up here whenever you need the exact details.