Practice Examples (25.3) - List Comprehension - Data Structures and Algorithms in Python
Students

Academic Programs

AI-powered learning for grades 8-12, aligned with major curricula

Professional

Professional Courses

Industry-relevant training in Business, Technology, and Design

Games

Interactive Games

Fun games to boost memory, math, typing, and English skills

Examples

Practice - Examples

Learning

Practice Questions

Test your understanding with targeted questions

Question 1 Easy

What does the following list comprehension generate? [x ** 2 for x in range(5)]

💡 Hint: Think of squaring numbers from 0 to 4.

Question 2 Easy

How can you use filter to get only odd numbers from a list?

💡 Hint: Remember the modulo operator to check for odd numbers.

4 more questions available

Interactive Quizzes

Quick quizzes to reinforce your learning

Question 1

What does the following code snippet return: list(map(lambda x: x**3, [1, 2, 3]))?

[1
2
3]
[1
4
9]
[1
8
27]

💡 Hint: Think of the operation of raising to the power of 3.

Question 2

Is it true that filter function can only return a list?

True
False

💡 Hint: Recall the output of functional programming functions.

2 more questions available

Challenge Problems

Push your limits with advanced challenges

Challenge 1 Hard

Create a list comprehension that generates all non-repeating Pythagorean triples from numbers 1 to 50.

💡 Hint: Remember to prevent duplicates by adjusting the ranges.

Challenge 2 Hard

Write a single-line comprehension that creates a list of tuples representing (number, square) for the first ten natural numbers.

💡 Hint: Think about how you can use grouping within parentheses.

Get performance evaluation

Reference links

Supplementary resources to enhance your learning experience.