DEV Community

Abdul Haseeb Mari
Abdul Haseeb Mari

Posted on

Python List Comprehensions: Write Cleaner and More Readable Code

Python is known for its readable syntax, and list comprehensions are one of the features that help make code more concise.

A list comprehension allows developers to create a new list from an existing iterable using a compact expression.

Let's explore how it works.

1. Creating a list with a traditional loop

Suppose we want to create a list containing the squares of numbers.

numbers = [1, 2, 3, 4, 5]
squares = []

for number in numbers:
    squares.append(number ** 2)

print(squares)
Enter fullscreen mode Exit fullscreen mode

Output:

[1, 4, 9, 16, 25]
Enter fullscreen mode Exit fullscreen mode

This works perfectly, but Python offers a shorter alternative.

2. Using a list comprehension

numbers = [1, 2, 3, 4, 5]

squares = [number ** 2 for number in numbers]

print(squares)
Enter fullscreen mode Exit fullscreen mode

The output is the same:

[1, 4, 9, 16, 25]
Enter fullscreen mode Exit fullscreen mode

The general structure is:

[expression for item in iterable]
Enter fullscreen mode Exit fullscreen mode

3. Adding conditions

You can also filter elements while creating a list.

For example, selecting only even numbers:

numbers = [1, 2, 3, 4, 5, 6, 7, 8]

even_numbers = [
    number
    for number in numbers
    if number % 2 == 0
]

print(even_numbers)
Enter fullscreen mode Exit fullscreen mode

Output:

[2, 4, 6, 8]
Enter fullscreen mode Exit fullscreen mode

4. Transforming strings

List comprehensions are not limited to numbers.

names = ["alice", "john", "sara", "michael"]

capitalized_names = [
    name.capitalize()
    for name in names
]

print(capitalized_names)
Enter fullscreen mode Exit fullscreen mode

Output:

['Alice', 'John', 'Sara', 'Michael']
Enter fullscreen mode Exit fullscreen mode

5. When should you use them?

List comprehensions are useful when:

  • Transforming elements in a list.
  • Filtering data using simple conditions.
  • Creating new lists from existing collections.
  • Keeping short data-processing operations readable.

However, avoid writing extremely complicated comprehensions. If an expression contains multiple conditions or nested loops that are difficult to understand, a normal loop may be better.

Performance and readability

List comprehensions are often faster than equivalent loops that repeatedly call append() in CPython, but actual performance depends on the operation and workload.

The most important consideration is readable code.

Final takeaway

Good Python programming isn't about writing the shortest possible code. It's about writing code that is clear, maintainable, and easy for other developers to understand.

Try this challenge:

Create a list comprehension that takes the numbers from 1 to 20 and returns only numbers divisible by 3.

Share your solution in the comments!

Python #PythonProgramming #LearnPython #SoftwareDevelopment #Coding #Programming

Top comments (0)