3

I'm using this function that works well to give me a numpy array of all possible combinations of arrays

 arrs = np.array(np.meshgrid([1,2,3], [4,5,6], [7,8,9])).T.reshape(-1,3)

But what I would like to do is take the 3 lists and create the meshgrid parameter like this:

lst1=[1,2,3]
lst2=[4,5,6]
lst3=[7,8,9]

arrs = np.array(np.meshgrid(lst)).T.reshape(-1,3)

But how can I create lst from the 3 lists? If I do this

lst=[lst1,lst2,lst3]

all I get is an error. Thank you.

Allen
  • 722
  • 4
  • 13
  • 31

1 Answers1

5

You need to unpack the lst:

# notice the *
arrs = np.array(np.meshgrid(*lst)).T.reshape(-1,3)
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
  • For future reference, this method is called "iterable unpacking". See https://peps.python.org/pep-3132/ and https://stackoverflow.com/a/45346333/7473705 – nish-ant Jul 11 '23 at 08:55