Computer Science
Grade 9
20 min
List Comprehensions: Creating Lists with Elegance
Master list comprehensions for creating new lists based on existing iterables.
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Define what a list comprehension is and identify its components.
Convert a standard `for` loop that creates a list into a single-line list comprehension.
Use a conditional `if` statement within a list comprehension to filter elements.
Apply simple operations (like mathematical calculations) to elements within a list comprehension.
Read and accurately predict the output of Python code that uses list comprehensions.
Explain the benefits of using list comprehensions, such as conciseness and readability.
Ever wanted to write 5 lines of code in just one? 🚀 What if you could build a list of numbers, filter it, and transform it all at once?
This lesson introduces list comprehensions, a powerful Python feature for creating lists in a single, readable line. You&...
2
Key Concepts & Vocabulary
TermDefinitionExample
List ComprehensionA concise, one-line syntax for creating a new list based on an existing list or another iterable.`squares = [x**2 for x in range(5)]` creates the list `[0, 1, 4, 9, 16]`.
IterableAny Python object that you can loop over, one item at a time. Common examples include lists, strings, and the output of `range()`.In `[c for c in 'hello']`, the string `'hello'` is the iterable.
ExpressionThe part of the list comprehension that specifies what to do with each item. This is what goes into the new list.In `[x * 10 for x in numbers]`, the expression is `x * 10`.
Iteration VariableA temporary variable that holds the current item from the iterable as the list comprehension loops through it.In `[num for num in my_list]`, `num` is the iteration...
3
Core Syntax & Patterns
Basic List Comprehension Syntax
new_list = [expression for item in iterable]
This is the simplest form. It iterates through each `item` in the `iterable` and applies the `expression` to it, adding the result to the `new_list`.
List Comprehension with a Filter
new_list = [expression for item in iterable if condition]
This adds a conditional check. The `expression` is only applied and its result added to the `new_list` if the `condition` for that `item` is true.
The 'For Loop' Equivalent
new_list = []
for item in iterable:
if condition:
new_list.append(expression)
This shows how a list comprehension is a compact version of a standard for loop. Understanding this helps you convert between the two forms.
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
A list comprehension with an `if` clause, like `[x for x in data if condition]`, is a concise, single-line way to achieve the same result as which two programming steps?
A.Defining a function and then calling it.
B.Sorting a list and then reversing it.
C.Looping through an iterable and appending to a new list based on a condition.
D.Creating a variable and assigning it a new value.
Challenging
Consider the code: `numbers = [10, 11, 12, 13, 14, 15]`. What is the output of `[numbers[i] for i in range(len(numbers)) if i % 2 == 0]`?
A.[10, 12, 14]
B.[0, 2, 4]
C.[11, 13, 15]
D.This code will produce an error.
Challenging
A programmer writes `result = [x for x in range(10) if x > 5]`. Another writes `result = [x if x > 5 else None for x in range(10)]`, intending to filter out `None` later. Based on the tutorial's principles of conciseness and readability for *filtering*, which approach is better and why?
A.The second is better because it is more explicit about what happens to the other numbers.
B.The first is better because it directly creates the filtered list in one step, making it more concise and readable.
C.Both are equally good as they can achieve the same final result.
D.The second is better because it uses a more advanced `if-else` structure.
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free