The comprehension consists of a single expression followed by at least one for
clause and zero or more for
or if
clauses.
There are three comprehensions in Python.
Types of comprehensions in Python
List comprehensions provide a concise way to create lists. Common applications are to make new lists where each element is the result of some operations applied to each member of another sequence or iterable, or to create a subsequence of those elements that satisfy a certain condition.
Syntax:
**[expression for item in iterable if conditional]**
Expression can be any arbitary expression,complex expressions, tuple , nested functions or other list comprehension.
This is equivalent to
for item in list:
if conditional:
expression
Return Type:
List
Using List Comprehension:
A list comprehension consists of brackets[]
containing an expression followed by a for
clause, then zero or more for
or if
clauses. The result will be a new list resulting from evaluating the expression in the context of the for
and if
clauses which follow it.
List Comprehension explained
#python #python3