Thomas Collett Thomas Collett - 17 days ago 5
Python Question

How to convert two lists into array coordinates in Python?

In Python, how do I build an array of coordinates given 2 lists of axes, such that the output array contains all the possible coordinate pairs?

e.g.

ax1=[1,3,4]
ax2=[a,b]

"""
Code that combines them
"""

Combined_ax1 = [1,3,4,1,3,4,1,3,4]
Combined_ax2 = [a,a,a,b,b,b,c,c,c]


I need this so I can feed combined_ax1 and combined_ax2 into a function without using multiple for loops.

Answer

This code would get what you require

import itertools

ax1=[1,3,4]
ax2=['a','b']

Combined_ax1, Combined_ax2 = zip(*itertools.product(ax1, ax2))
Comments