You can use Numpy column_stack() or row_stack() to combine lists into an array.
As Columns:
np.column_stack((list1, list2,…))
As Rows:
np.row_stack((list1, list2,…))
Example 1 of lists to columns
The following combines lists into an array using column_stack()
. Thus, lists become columns in the array.
# create lists
City1= [6,2,3,4,5]
City2= [2,1,3,4,5]
City3= [4,1,2,4,5]
# use Numpy column_stack() to combine lists into array columns
import numpy as np
combined_array=np.column_stack((City1, City2,City3))
# print out the combined array
print(combined_array)
[[6 2 4] [2 1 1] [3 3 2] [4 4 4] [5 5 5]]
Example 2 of lists to rows
The following combines lists into an array using row_stack()
. Thus, lists become rows in the array.
# create lists
City1= [6,2,3,4,5]
City2= [2,1,3,4,5]
City3= [4,1,2,4,5]
# use Numpy row_stack() to combine lists into array rows
import numpy as np
combined_array=np.row_stack((City1, City2,City3))
# print out the combined array
print(combined_array)
[[6 2 3 4 5] [2 1 3 4 5] [4 1 2 4 5]]