DEV Community


Posted on

Python list comprehension

Ever use list comprehension in Python?

If not here is very beautiful use of list comprehension in Python which will make your code more neat and readable.

First of all what is list comprehension?

List comprehension is a syntax, list comprehension offer a shorter syntax when you want to create a new list based on the values of an existing list.

Here is the SYNTAX for it
[expression for item in iterable if condition == True]

Below I have shown two examples
One without using list comprehension and another with list comprehension.

Without list comprehension
Alt Text

With list comprehension
Alt Text

Discussion (1)

sahilsaif1221 profile image
Sahil Saif

Awesome 😎