Supersonic
Supersonic

Reputation: 430

Merge multiple arrays using zip

This may be a silly one. But I am not able to figure it out.

names = ['Fred', 'John', 'Mark']

age = [27, 40, 25]

location = ['Sweden', 'Denmark', 'Poland']

names.zip(age) 

#Outputs => [["Fred", 27], ["John", 40], ["Mark", 25]]

But I need to output it with the 3rd array (location) with it.

#Expected Output => [["Fred", 27,"Sweden"], ["John", 40,"Denmark"], ["Mark", 25,"Poland"]]

The most important condition here is that, there may be any number of arrays but the output should form from the first element of each array and encapsulate inside another array.

Thanks for any help.

Upvotes: 2

Views: 358

Answers (1)

Surya
Surya

Reputation: 16012

Try passing multiple arguments to zip:

names.zip(age, location)
# => [["Fred", 27, "Sweden"], ["John", 40, "Denmark"], ["Mark", 25, "Poland"]]

Upvotes: 3

Related Questions