# Creating a 2d list out of inputs [duplicate]

394
March 13, 2017, at 7:27 PM

• How do you split a list into evenly sized chunks? 50 answers

I'm working on a gameboard that takes in inputs and creates a board out of them and to edit the board I want to make it a 2d list

When the user inputs:

. . . .

. B W .

. W B .

. . . .

My function

def starting_board(rows: int) -> list: board = [] for i in range(rows): board.append(input().replace(' ', '')) return board

Returns this: ['....', '.BW.', '.WB.', '....']

But I want it to return: [['.', '.', '.', '.'], ['.', 'B', 'W', '.'], etc...]

This will work:

``````>>> def to_matrix(l, n):
return [l[i:i+n] for i in xrange(0, len(l), n)]
>>> l = [0,1,2,3]
>>> to_matrix(l,2)
>>> [[0, 1], [2, 3]]
``````

Hope it helps

A functional aproach:

``````l = [1,2,3,4,5,6,7,8]
map(list, zip(l[::2], l[1::2]))
[[1, 2], [3, 4], [5, 6], [7, 8]]
``````

Also with `iter`:

``````it = iter(l)
zip(it, it)
[(1, 2), (3, 4), (5, 6), (7, 8)]
``````
POPULAR ONLINE

### Error if scipy is frozen into an executable with cx-Freeze

at the moment I am facing a problem regarding scipy and cx-Freeze

530

### Pip doesn't want to upgrade [on hold]

When I try to upgrade pip by using

331

### Calculating row-wise delta values in a DataFrame

I'm trying to calculate what I am calling "delta values", meaning the amount that has changed between two consecutive rows

438

### How to handle exception in using(Py.GIL()) block pythonnet

Is there a way to handle exception in using(PyGIL()) block?

631