List Comprehension
Hello guys, here we will talk about list comprehension.
List comprehension is a precise way to create a new
list from the existing list in python.
Syntax is,
[expression for item in list if conditional]
This is equivalent to,
for
item in list:
if conditional:
expression
Output
Let’s go through the statement,
new_list:- creates a new
list.
i +
(i+2):- expression, based on variable used in the old list.
for i
in range (8):- iterating from 0 to 8 (can imagine it a list)
if i%2==0:-
if condition for the new list
No comments:
Post a Comment