0

I have 3 lists and want to merge them as a list by using map not just zip.

a=[1,1,1,1]
b=[2,2,2,2]
c=[3,3,3,3]

I want to obtain list bellow

f=[[1,2,3],[1,2,3],[1,2,3],[1,2,3]]

i am using python3 would you please educate me how can i do?

Bezi
  • 45
  • 7
  • 1
    So, what have you tried? Also, is it safe to assume that the 3 lists have the same length? – Andreas Nov 19 '18 at 08:32
  • by using zip(a,b,c) i can get final list. but its not as a list, and also according to this page https://stackoverflow.com/questions/35503922/merging-lists-of-lists/53370703#53370703 , i have tried to do it but i got some errors about zip and map. it looks in python3 they do not work as pervius version. i am a beginner in python – Bezi Nov 19 '18 at 08:36
  • yes all the lists have same lenght – Bezi Nov 19 '18 at 08:39
  • `list(map(list,zip(a,b,c)))` – Onyambu Nov 19 '18 at 08:41
  • `[list(i) for i in zip(a,b,c)]` – Onyambu Nov 19 '18 at 08:42
  • thank you :) , yes i also made a zip then transform to list style but yours is a shorter way, great ! – Bezi Nov 23 '18 at 06:46

1 Answers1

0

You can do it with a simple loop. Loop through the index and create a list of the elements for each list at that particular index. Then append that list to the f.

f = []
for i in range(len(a)):
    f.append([a[i], b[i], c[i]])
print(f)
Andreas
  • 2,455
  • 10
  • 21
  • 24