2D array from list of values

Viewed 26

I have a list 768 values long and need to make a 32*24 array, I have looked at other answers and questions but can't find any like this.

# Example 36 value long list needing to be a 6*6 array

values = [1,2,3,4,5,6,7,8,9,10....36]

# Missing code I can't figure out

array = [[1,2,3,4,5,6],[7,8,9,10,11,12],[13,14,15,16,17,18],[19,20,21,22,23,24],[25,26,27,28,29,30],[31,32,33,34,35,36]]

Thank you for any help or comments

1 Answers

You can use numpy to do this:

import numpy as np
values = list(range(1, 37))
array = np.array(values).reshape((6, 6))

The result is

array([[ 1,  2,  3,  4,  5,  6],
       [ 7,  8,  9, 10, 11, 12],
       [13, 14, 15, 16, 17, 18],
       [19, 20, 21, 22, 23, 24],
       [25, 26, 27, 28, 29, 30],
       [31, 32, 33, 34, 35, 36]])
Related