Python Comprehensions - Learn Python.
Updated: Feb 22
One of the cool features of Python is comprehension. Comprehensions are Syntactic Sugar of Python. It looks simple and more efficient. In Python, list comprehension is more popular. We'll see the explanation using the list comprehension. There are three kinds of comprehension in Python, such as List comprehension, Dictionary comprehension, and Set comprehension. Otherwise, we can use comprehension syntax in a generator expression. We'll see all kinds of comprehensions in this post.
Sugar refers to sweet most of the people do not hate sweet. Syntactic sugar is a syntax that is sweeter to programmers. The syntax is easily readable to Humans.
Step by Step Explanation
First, we'll see the list comprehension.
In the above example, list comprehension helps to reduce the number of lines in code. The syntax is easy to write. We can do a map and filter in the same list comprehension.
We can create a nested list using comprehension syntax.
Set Comprehension & Generator Comprehension
I hope this helps. Thank you for visiting TechAaroorian.
Check Python-related blogs here.