python list comprehension examples programs
Example 1: simple list comprehension example
>>> squares = [i * i for i in range(10)]
>>> squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Example 2: list comprehension in python
Squaring List elements:
By loop:
lst = [1, 2, 3]
l =[]
for i in lst:
lst.append(i*i)
print(l)
By List Comprehension:
lst = [1, 2, 3]
l = [x*x for x in lst]
print(l)