๐Ÿ“– Beginning Python Programming /
Module: List & Collection

List comprehension

๐Ÿ“– Beginning Python Programming / List & Collection / List comprehension

Different approaches for list transformation

If we want to use transform a given list L into square value for each element.

L = range(1,10)

Approach 1: Just printing the calculation

for x in L:
  print(x*x)

Approach 2: Using list append

L2 = []
for x in L:
  L2.append( x*x )
print(L2)

Approach 3: Using map

L3 = list( map(lambda x: x*x, L))
print(L3)

Approach 4: List comprehension

L4 = [x*x for x in L]
print(L4)