Arpan Das
Arpan Das

Reputation: 339

Grouping the elements of arrays in python

I have 3 different arrays in python like

a = [1,2,3]
b = [6,7,8 ]
c = [20,21,22]

And I want to create an array which will group the elements from the 3 arrays like

array = [[1,6,20],[2,7,21],[3,8,22]]

What is the easiest way to do it? Any help? Thank you in advance

Upvotes: 0

Views: 128

Answers (2)

Garrett R
Garrett R

Reputation: 2662

You can do this with zip and map in one line of code.

map(list, zip(a,b,c))

Upvotes: 1

AlokThakur
AlokThakur

Reputation: 3741

You can use zip to create desire result, But zip will make list of tuples, So using list comprehension on top of it.

[list(x) for x in zip(a,b,c)]

Upvotes: 3

Related Questions