List Comprehension
Welcome to the Tutorial of Python
In this tutorial we will try to understand what is list comprehension
List comprehension is an easier way to define and create list in python. We can create lists just like mathematical statements and in one line only.
A list comprehension generally consist of these parts : Output expression, input sequence, a variable representing member of input sequence and an optional predicate part.
Eg:
Normal List Creation
|
List Comprehension
|
Out = []
For num in range(1,11):
If(num%2 == 0):
Out.append(num**2)
|
Out =[x ** 2 for x in range
(1, 11) if x % 2 == 0]
|
List Comprehension
Reviewed by Analytics Pundit
on
November 29, 2019
Rating:
No comments: