How to print out a 2d list that is formatted into a grid?

2d, list, python

Solution

If you want to "pretty" print your grid with each sublist on its own line, you can use `pprint`:

>>> grid=[[['o'], ['-'], ['-'], ['-'], ['-']], [['-'], ['-'], ['-'], ['-'], ['-']], [['-'], ['-'], ['-'], ['-'], ['-']]]

>>> from pprint import pprint
>>> pprint(grid)

[[['o'], ['-'], ['-'], ['-'], ['-']],
 [['-'], ['-'], ['-'], ['-'], ['-']],
 [['-'], ['-'], ['-'], ['-'], ['-']]]

It will still show each element as a list, as you defined it, if you want to show them as strings you'll have to use joins like m.wasowski suggests.

Problem

Currently, I have made this code ``` def grid_maker(h,w): grid = [[["-"] for i in range(w)] for i in range(h)] grid[0][0] = ["o"] print grid >>>grid_maker(3,5) => [[['o'], ['-'], ['-'], ['-'], ['-']], [['-'], ['-'], ['-'], ['-'], ['-']], [['-'], ['-'], ['-'], ['-'], ['-']]] ``` I want to make another function that will take in the produced 2d array and print it out such that it is in this format: ``` o---- ----- ----- ``` However, I am unsure where to start.

Original source