Explore programming tutorials, exercises, quizzes, and solutions!
Python List Comprehensions Exercises
1/20
As you continue learning Python, you discover a compact and expressive way to create new lists called list comprehensions. They allow you to build lists in a single line by combining loops and conditions. This makes your code cleaner, more readable, and often faster than using traditional for loops.
Which of the following examples correctly uses a list comprehension to create a list of squares of numbers from 1 to 5?
List comprehensions in Python use square brackets and follow the format:
[expression for item in iterable]
In option 2:
squares = [x * x for x in range(1, 6)]
This creates a list: [1, 4, 9, 16, 25].
Option 1 is invalid syntax.
Option 3 creates a generator expression, not a list.
Option 4 uses incorrect syntax and keywords.
List comprehensions are ideal when you want to transform or filter items from an iterable in a concise and Pythonic way.
What will be the output of the following code?
lst = [1, 2, 3, 4, 5]
result = [x for x in lst if x % 2 == 0]
print(result)
The list comprehension [x for x in lst if x % 2 == 0] filters the list and includes only even numbers. Therefore, the output is [2, 4].
What will be the output of the following code?
lst = [1, 2, 3, 4, 5]
result = [x * x for x in lst]
print(result)
The list comprehension [x * x for x in lst] calculates the square of each element in the list. Therefore, the output is [1, 4, 9, 16, 25].
What will be the output of the following code?
lst = [1, 2, 3, 4, 5]
result = [x for x in lst if x > 3]
print(result)
The list comprehension [x for x in lst if x > 3] filters the list to include only numbers greater than 3. Therefore, the output is [4, 5].
What will be the output of the following code?
lst = ['apple', 'banana', 'cherry']
result = [x.upper() for x in lst]
print(result)
The list comprehension [x.upper() for x in lst] converts each string in the list to uppercase. Therefore, the output is ['APPLE', 'BANANA', 'CHERRY'].
What will be the output of the following code?
lst = [2, 4, 6, 8, 10]
result = [x - 1 for x in lst if x % 2 == 0]
print(result)
The list comprehension [x - 1 for x in lst if x % 2 == 0] first filters the elements in lst that are even, and then subtracts 1 from each even number. The even numbers in lst are [2, 4, 6, 8, 10], so the result will be [1, 3, 5, 7, 9].
What will be the output of the following code?
lst = [1, 2, 3, 4, 5]
result = [str(x) + 'a' for x in lst]
print(result)
The list comprehension [str(x) + 'a' for x in lst] converts each number in lst to a string and appends the letter 'a'. The result will be ['1a', '2a', '3a', '4a', '5a'].
What will be the output of the following code?
lst = [10, 20, 30, 40, 50]
result = [x / 5 for x in lst if x % 10 == 0]
print(result)
The list comprehension [x / 5 for x in lst if x % 10 == 0] filters the list to only include numbers divisible by 10, and then divides each of them by 5. The numbers divisible by 10 in lst are [10, 20, 30, 40, 50], and dividing each by 5 results in [2.0, 4.0, 6.0, 8.0, 10.0].
What will be the output of the following code?
lst = ['hello', 'world', 'python', 'rocks']
result = [x[::-1] for x in lst]
print(result)
The list comprehension [x[::-1] for x in lst] reverses each string in lst. The result will be ['olleh', 'dlrow', 'nohtyp', 'skcor'].
What will be the output of the following code?
lst = [3, 6, 9, 12, 15]
result = [x // 3 for x in lst if x % 6 == 0]
print(result)
The list comprehension [x // 3 for x in lst if x % 6 == 0] first filters the elements in lst that are divisible by 6, and then performs integer division by 3. The numbers divisible by 6 in lst are [6, 12], and performing integer division by 3 results in [2, 4].
What will be the output of the following code?
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
result = [x * y for x in lst for y in lst if x != y]
print(result)
The list comprehension [x * y for x in lst for y in lst if x != y] creates all possible pairs of numbers from the list and multiplies them. It excludes the cases where x == y (since x != y is the condition). This results in a long list of products from the pairwise combinations of numbers in the list, which starts with [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, ...].
What will be the output of the following code?
lst = [1, 2, 3, 4, 5]
result = [x * y for x in lst for y in lst if x + y == 6]
print(result)
The list comprehension [x * y for x in lst for y in lst if x + y == 6] multiplies the numbers x and y for each combination of values from the list lst where the sum of x + y equals 6. The pairs that satisfy this condition are (1, 5), (2, 4), and (3, 3), so the result will be [5, 8, 12].
What will be the output of the following code?
lst = ['apple', 'banana', 'cherry', 'date', 'elderberry']
result = [x[0:3] for x in lst if len(x) > 5]
print(result)
The list comprehension [x[0:3] for x in lst if len(x) > 5] extracts the first three letters of the strings from lst where the length of the string is greater than 5. The words in lst that have more than 5 characters are ['banana', 'cherry', 'elderberry'], and the first three letters of these are ['ban', 'che', 'eld']. Therefore, the output will be ['app', 'ban', 'che', 'eld'].
What will be the output of the following code?
lst = [3, 6, 9, 12, 15]
result = [x for x in lst if x % 3 == 0 and x % 5 == 0]
print(result)
The list comprehension [x for x in lst if x % 3 == 0 and x % 5 == 0] filters the elements in lst to include only those that are divisible by both 3 and 5. The only number in lst that meets this condition is 15, so the result will be [15].
What will be the output of the following code?
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
result = [x * x if x % 2 == 0 else x * 3 for x in lst]
print(result)
The list comprehension [x * x if x % 2 == 0 else x * 3 for x in lst] squares each even number and multiplies each odd number by 3. For example: 1 is odd, so it becomes 1 * 3 = 3; 2 is even, so it becomes 2 * 2 = 4; 3 is odd, so it becomes 3 * 3 = 9, and so on. The final result will be [3, 4, 9, 16, 15, 36, 21, 64, 27, 100].
What will be the output of the following code?
lst = [5, 10, 15, 20, 25]
result = [x for x in lst if x % 2 == 0 or x % 5 == 0]
print(result)
The list comprehension [x for x in lst if x % 2 == 0 or x % 5 == 0] selects the elements from lst that are either divisible by 2 or by 5. Since the numbers 5, 10, 15, 20, and 25 all meet one of these conditions, the result will be [5, 10, 15, 20, 25].
What will be the output of the following code?
lst = ['apple', 'banana', 'cherry', 'date', 'elderberry']
result = [x.upper() for x in lst if len(x) > 5]
print(result)
The list comprehension [x.upper() for x in lst if len(x) > 5] converts to uppercase the words in lst that have more than 5 characters. In this case, 'banana', 'cherry', and 'elderberry' meet this condition. Therefore, the output will be ['BANANA', 'CHERRY', 'ELDERBERRY'].
What will be the output of the following code?
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
result = [x * x if x % 2 == 0 else x * 3 for x in lst if x % 2 != 0]
print(result)
The list comprehension [x * x if x % 2 == 0 else x * 3 for x in lst if x % 2 != 0] applies a conditional operation: for odd numbers (where x % 2 != 0), it multiplies them by 3; for even numbers, it squares them. But since the list comprehension includes a filter for odd numbers, the even numbers are excluded. The resulting list for odd numbers 1, 3, 5, 7, and 9 will be [3, 9, 27, 49, 81].
What will be the output of the following code?
lst = [3, 5, 7, 9, 11, 13]
result = [x for x in lst if x % 2 == 0 and x % 3 == 0]
print(result)
The list comprehension [x for x in lst if x % 2 == 0 and x % 3 == 0] selects numbers from lst that are divisible by both 2 and 3. There are no numbers in lst that meet both conditions, so the result will be an empty list [].
What will be the output of the following code?
lst = ['apple', 'banana', 'cherry', 'date', 'elderberry']
result = [len(x) for x in lst if 'a' in x]
print(result)
The list comprehension [len(x) for x in lst if 'a' in x] returns the lengths of the words in lst that contain the letter 'a'. The words 'apple', 'banana', 'cherry', and 'elderberry' all contain 'a', so their lengths will be [5, 6, 6]. Therefore, the result will be [5, 6, 6].
Practicing Python List Comprehensions? Don’t forget to test yourself later in
our
Python Quiz.
About This Exercise: Python – List Comprehensions
Welcome to the Python List Comprehensions exercises — a carefully crafted set of challenges focused on helping you master one of Python’s most elegant and powerful features. List comprehensions provide a concise and readable way to create and transform lists, making your code cleaner, faster, and more Pythonic. Whether you’re a beginner looking to understand this concept or an experienced programmer wanting to write more efficient code, this section will guide you step-by-step through list comprehension techniques.
List comprehensions allow you to generate new lists by applying expressions and conditions to existing iterables, all within a single, readable line of code. This feature is widely used in Python programming because it combines clarity and performance, often replacing traditional loops with a more compact and expressive syntax. Through these exercises, you’ll learn how to leverage list comprehensions to simplify your code without sacrificing readability.
Starting from basic syntax, these exercises will introduce you to constructing list comprehensions that iterate over lists, ranges, or other iterables. You’ll practice incorporating conditional statements to filter elements and apply transformations directly within the comprehension. As you progress, you’ll encounter more advanced topics like nested list comprehensions, using multiple for-loops, and even combining comprehensions with functions for powerful data processing.
Understanding list comprehensions is essential for anyone aiming to write efficient Python code, especially in fields like data science, web development, automation, and algorithm design. They help reduce boilerplate code, improve execution speed, and make your logic easier to follow. Many Pythonic solutions and interview questions rely on this knowledge, making it a critical skill for technical assessments and real-world projects alike.
Along with hands-on coding exercises, this section emphasizes best practices such as maintaining readability, avoiding overly complex comprehensions, and understanding when to use list comprehensions versus other data structures like generator expressions or traditional loops. This balanced approach ensures you not only write code that works but code that other developers can easily understand and maintain.
Additionally, we encourage you to explore related Python features such as dictionary and set comprehensions, lambda functions, and functional programming tools like map and filter. Together, these concepts will deepen your understanding of Python’s expressive power and help you write concise, high-performance programs.
Start practicing the Python List Comprehensions exercises today to unlock a new level of coding elegance and efficiency. With regular practice, you’ll soon be able to tackle complex data transformation tasks with simple, readable one-liners — a hallmark of proficient Python programmers.