DEV Community

SAIFULLAH🇮🇳
SAIFULLAH🇮🇳

Posted on

3

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

Top comments (1)

Collapse
 
sahilsaif1221 profile image
Sahil Saif

Awesome 😎

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay