List Comprehension in Python
List comprehension is a efficient way of creating a list in one line using for loop.Structure of list comprehension:
list_name = [ expression | for loop | condition ]
Example: If we want to find even number from a list , we can do that using list comprehension easily
list_number = [1,2,3,4,5,6,7,8,9,10]
list_even = [ x for x in list_number if x%2==0]
print(list_even)
output:[2,4,6,8,10]
list_name = [ expression | for loop | condition ]
Example: If we want to find even number from a list , we can do that using list comprehension easily
list_number = [1,2,3,4,5,6,7,8,9,10]
list_even = [ x for x in list_number if x%2==0]
print(list_even)
output:[2,4,6,8,10]
Comments
Post a Comment