DEV Community

Njeri Kimaru
Njeri Kimaru

Posted on

List Comprehension vs. Dictionary Comprehension in Python

Python makes it easy to write clean and compact code using comprehensions. But what's the difference between list comprehension and dictionary comprehension?

Let’s break it down like a Pythonista.

📝 List Comprehension

Used to build lists from iterables.


python
squares = [x**2 for x in range(5)]
print(squares)  # [0, 1, 4, 9, 16]
Enter fullscreen mode Exit fullscreen mode

Top comments (0)