We have a list a = [1, 2, 3, 4] and we need to create another list with squares of a. That is the output to be [1, 4, 9, 16].
a = [1,2,3,4]
Loop method
b = []
for i in a:
b.append(i**2)
OUTPUT
[1, 4, 9, 16]
List Comprehension Method
c = [i**2 for i in a]
OUTPUT
[1, 4, 9, 16]
Comments
Post a Comment