0

I've got two lists x and y. Want to combine them in a way:

 x_y = [[x[0], y[0]], [x[1], y[1]]...].

The code I have is:

import numpy as np

x_ax = np.array(list(range(1, 101)))
y_ax = np.array(list(range(101,201)))

mixed = []
mini = []
for a, b in zip(x_ax, y_ax):
    mini = [a,b]
    mixed.append(mini)

print(mixed)

I am sure there are better ways to do it. Thanks for help!

mhhabib
  • 2,975
  • 1
  • 15
  • 29

1 Answers1

1

You can do np.stack. This will return a 2D numpy array

np.stack([x_ax, y_ax]).T

Or you can use zip. This will return a list of tuples.

list(zip(x_ax,y_ax))
Akshay Sehgal
  • 18,741
  • 3
  • 21
  • 51