DEV Community

Samiksha Srivastav
Samiksha Srivastav

Posted on

The Python Feature That Instantly Felt Magical to Me... List Comprehensions

When I first started learning Python, one thing immediately stood out to me:

squares = [x*x for x in range(5)]

I remember staring at this for a second like:

“Wait… a loop INSIDE a list??”

Coming from Java and JavaScript, this felt surprisingly different and honestly very elegant.

That’s when I discovered one of Python’s most loved features:

List Comprehensions


What is a List Comprehension?

A list comprehension is a compact way of creating lists using loops.

Instead of writing:

nums = []
**
**for i in range(5):

nums.append(i*i)

print(nums)

You can simply write:

nums = [i*i for i in range(5)]

print(nums)

Output:

[0, 1, 4, 9, 16]

Same result.

Much cleaner syntax.


Why It Feels So Interesting

In many languages, loops and list creation are usually written separately.

But Python allows you to:

  • loop
  • transform
  • filter
  • create lists

all in a single elegant expression.

That’s what makes list comprehensions feel so powerful.


General Syntax

[expression for item in iterable]


Simple Example

nums = [x for x in range(5)]

print(nums)

Output:

[0, 1, 2, 3, 4]


Applying Operations While Looping

squares = [x*x for x in range(5)]

print(squares)

Output:

[0, 1, 4, 9, 16]


Adding Conditions

This is where it gets even cooler

evens = [x for x in range(10) if x % 2 == 0]

print(evens)

Output:

[0, 2, 4, 6, 8]

Python is:

  • looping
  • checking condition
  • building list

all at once.


Real-World Feeling

List comprehensions make Python code feel:

  • expressive
  • readable
  • concise
  • almost sentence-like

Example:

names = ["python", "java", "javascript"]

caps = [name.upper() for name in names]

print(caps)

Output:

['PYTHON', 'JAVA', 'JAVASCRIPT']


Nested List Comprehension

Python can even do nested loops:

pairs = [(x, y) for x in range(2) for y in range(2)]

print(pairs)

Output:

[(0, 0), (0, 1), (1, 0), (1, 1)]

This is equivalent to:

pairs = []

for x in range(2):
for y in range(2):
** pairs.append((x, y))**


Why Python Developers Love It

List comprehensions are heavily used in:

  • data processing
  • automation
  • APIs
  • machine learning
  • backend development
  • scripting

because they reduce boilerplate code.


But There’s Also a Catch

Just because list comprehensions are compact doesn’t mean they should become unreadable.

Example of overdoing it:

result = [x*y for x in range(10) if x % 2 == 0 for y in range(5)]

Sometimes a normal loop is clearer.

Readable code > clever code.


My Favorite Part About Python

Features like:

  • list comprehensions
  • slicing
  • unpacking
  • f-strings

make Python feel incredibly developer-friendly.

And honestly, list comprehensions were one of the first Python features that made me think:

“Okay… this language is actually really fun.”

Happy Coding

Top comments (0)