# List comprehension
list_comp = [i+3 for i in range(20)]
# above code is similar to
for i in range(20):
print(i + 3)
# Python program to demonstrate list
# comprehension in Python
# below list contains square of all
# odd numbers from range 1 to 10
odd_square = [x ** 2 for x in range(1, 11) if x % 2 == 1]
print (odd_square)
list = [[2,4,6,8]]
matrix = [[row[i] for row in list] for i in range(4)]
print(matrix)
1
x=[i for i in range(5)]
# List Comprehension
# If one variable
power = [x**2 for x in range(1, 10)]
# If need two variables especially
# when you work Dictionary with List Comprehension.
# create a simple (key, value), tuple for context variables x and y
l_Comp = [(x,y) for x, y in employees.items() if y >= 100000]
[expr for val1 in collection1 and val2 collection2 if(condition)]
[col for col in df.columns]
# for understanding, above generation is same as,
odd_square = []
for x in range(1, 11):
if x % 2 == 1:
odd_square.append(x**2)
print (odd_square)