Home > Net >  expected result from 'map' not getting
expected result from 'map' not getting

Time:12-21

I expect to get the following result but i cant. can anyone help? [[2, 4, 6], [8, 10, 12], [14, 16, 18], [20, 22, 24]]

A=[]
B=[]
C=[]

lst = [ [1,2,3], [4,5,6], [7,8,9], [10,11,12] ]  

for A in lst:

    B = list(map(lambda x=B : x*2 for B in A , A)
    C.append(B)

print(C)

CodePudding user response:

lst = [ [1,2,3], [4,5,6], [7,8,9], [10,11,12] ]

If you want to keep your initial approach:

A=[]
C=[]

for A in lst:
    B = list(map(lambda x : x*2 , A))
    C.append(B)

print(C)

A simpler way to do it:

A=[]
C=[]

for A in lst:
    B = [x*2 for x in A]
    C.append(B)

print(C)

And even more simply, using list comprehension:

C = [[x*2 for x in B] for B in lst]

print(C)
  • Related