Python:Comprehension
lastmod 2020-11-24created 2020-06-123 edits
A comprehension is one way to create an iterable object, and one of the useful features available in Python.
List Comprehension (LC)
A list comprehension is a way to create lists easily.
Set Comprehension (SC)
A set comprehension is exactly the same as an LC; the only difference is that it creates a set instead of a list.
Dict Comprehension (DC)
A dict comprehension is also the same as an LC, and creates a dict.
Generator Expression (GE)
A generator expression is a special form of comprehension. It creates a generator that returns one element at a time instead of all elements at once.
Example
Using a conditional inside a for statement creates a 2-depth block.
With Python's list comprehension, you can handle a for statement and an if statement at once in a single line.