DEV Community

Cover image for Python List Comprehension – Understand the Logic & Benefit Behind
Sona
Sona

Posted on

Python List Comprehension – Understand the Logic & Benefit Behind

Python is praised for its ability to create code that is both elegant and easy to understand. One of its standout features is list comprehension, which lets us write powerful functionality in just a single line. However, some Python developers find it challenging to use list comprehension to its full potential. Overusing list comprehension can lead to less efficient and harder-to-read code.

Example of List comprehension in Python

List Comprehension syntax below followed by a example how to use it

newlist = [expression for item in iterable if condition == True]  
Enter fullscreen mode Exit fullscreen mode
Person = ["Priya", "Hira", "Rudra", "Badsha", "Lion"]  
newlist = [x for x in Person if "i" in x]  
print(newlist)  
Enter fullscreen mode Exit fullscreen mode

Read More

Top comments (0)