LeetCampus
Interview Question

What is List Comprehension? Give an Example.

July 24, 2025
0 views
Difficulty: Easy
Popularity: Common
Share on

Question Explanation

List comprehension is a concise way to create lists in Python. It allows you to generate a new list by applying an expression to each item in an existing iterable (like a list or a range). Interviewers often ask about list comprehension because it demonstrates a candidate's understanding of Python's functional programming capabilities and their ability to write efficient, readable code. Using list comprehension can lead to more compact code, which is easier to maintain and understand. It's important in real-world applications where you need to transform or filter data quickly. Common misconceptions include confusing list comprehension with traditional loops or not realizing when it's appropriate to use them. Additionally, understanding the performance implications and readability trade-offs of list comprehensions versus loops is crucial. Overall, mastery of this feature showcases a developer's proficiency in Python and their approach to problem-solving. Key points to remember include the syntax, use cases, and potential pitfalls when using list comprehension.

Sample Answers

Example 1: Basic List Comprehension

Let's start with a simple example. Suppose you want to create a list of squares for the numbers from 0 to 9. Instead of using a traditional loop, you can use list comprehension. Here’s how you can do it:

squares = [x**2 for x in range(10)]
print(squares)

This code will generate a list: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]. The expression x**2 is applied to each item in the iterable range(10), illustrating the power and simplicity of list comprehension. It's not only more readable but also often faster than using a loop.

Example 2: Filtering with List Comprehension

List comprehension can also include a condition to filter items. For example, if you want to create a list of even numbers from 0 to 20, you can do it like this:

even_numbers = [x for x in range(21) if x % 2 == 0]
print(even_numbers)

This will output: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]. Here, the condition if x % 2 == 0 filters the numbers, demonstrating how list comprehension can be used not just for transformation, but also for filtering data efficiently.

Example 3: Nested List Comprehension

List comprehension can also be nested for more complex structures. For example, if you want to create a 2D grid of coordinates, you can achieve this using nested list comprehension:

grid = [(x, y) for x in range(3) for y in range(3)]
print(grid)

This will produce: [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]. This example efficiently combines loops in a single line, showcasing the elegance and power of Python's list comprehension for generating complex data structures.

Keywords

Pythonlist comprehensiondata transformationfunctional programmingcoding efficiency

Ready to practice more questions?

Explore our collection of technical interview questions from top companies.

View All Questions