Python print 2d array as grid

I need to print this to look like:

....

....

....

currently I have this:

def main[]:
    rows=3
    col=4
    values=[[0,0,0,0],
            [0,0,0,0],
            [0,0,0,0]]
    for i in range[rows]:
        for j in range[col]:
            values[i][j]='.'
    print[values]
main[]

which will print [['.', '.', '.', '.'], ['.', '.', '.', '.'], ['.', '.', '.', '.']]

Is there a way to get it looking nicer?

asked Nov 26, 2014 at 2:01

4

This will work for any size of array with any type of values:

print['\n'.join[' '.join[str[x] for x in row] for row in values]]

Somewhat longer and much clearer:

  lines = []
  for row in values:
    lines.append[' '.join[str[x] for x in row]]
  print['\n'.join[lines]]

answered Nov 26, 2014 at 2:18

Michael LaszloMichael Laszlo

11.7k2 gold badges27 silver badges46 bronze badges

you can try like this:

for x in range[3]:
    print['.'*4]         # when you multiply a string with n, it produces n string

output

....
....
....

modification in your code:

def main[]:
    rows=3
    col=4
    values=[[0,0,0,0],
        [0,0,0,0],
        [0,0,0,0]]
    for i in range[rows]:
        print["".join['.' for j in range[col]]]

main[]

output:

....
....
....

answered Nov 26, 2014 at 2:07

HackaholicHackaholic

18.1k3 gold badges52 silver badges70 bronze badges

0

I would use *, the unpacking operator:

array_2d = [[1, 2, 3], [10, 20, 30], [100, 200, 300]]

for row in array_2d:
    print[*row, sep="\t"]

#output:
1   2   3
10  20  30
100 200 300

answered Oct 9, 2021 at 16:05

If you have no need for the values object, and were only building it in an attempt to make it easier to print… don't do that, just use Hackaholic's solution.

But if you actually need the values object, or you already have it and want to know how to print it, do it like this:

print['\n'.join[''.join[row] for row in values]]

Or, more explicitly:

for row in values:
    line = ''.join[row]
    print[line]

Or, even more explicitly:

for row in values:
    for col in row:
        print[col, end='']
    print[]

answered Nov 26, 2014 at 2:15

abarnertabarnert

341k44 gold badges573 silver badges651 bronze badges

If you don't need, or don't want to use an array or a for loop for that matter, here are a bunch of ways to do it.

# Dynamic and easy to customize, and also what I would use if I needed
# to print more than once.
def print_chars[char=".",n_of_chars=4,n_of_lines=4]:
    single_line = [char * n_of_chars] + '\n'
    print[single_line * n_of_lines]

print_chars[]

....
....
....
....

# or maybe you want 2 rows of 10 dashes?
print_chars['-',10,2]

----------
----------

# 2 rows of 5 smileys?
print_chars[':-] ',5,2]

:-] :-] :-] :-] :-] 
:-] :-] :-] :-] :-]


# If your only going to use it once maybe this
print[[['.' * 4] + '\n'] * 4]

# or this
print['....\n' * 4]

There is probably a way to do it faster or more pythonic, but hey. In the end your needs or coding style may be different, and i'll bet there are probably many more ways to do this exact same thing with python. You just have to remember that readability and speed are both your friends, but oftentimes they don't like each other.[although simple things like this are almost always easy to read in python].

well there's my 2 cents. :-]

answered Nov 26, 2014 at 18:55

You are printing a variable, which is an array, you need to print while on the loop. and you don't need an array full of 0.

for i in range [0, 5]:
    for j in range [0, 5]:
        print ".",
    print "\n"

answered Nov 26, 2014 at 2:09

CheluisCheluis

1,3723 gold badges20 silver badges50 bronze badges

4

How do you print a 2D array in python?

Insert.py.
# Write a program to insert the element into the 2D [two dimensional] array of Python..
from array import * # import all package related to the array..
arr1 = [[1, 2, 3, 4], [8, 9, 10, 12]] # initialize the array elements..
print["Before inserting the array elements: "].
print[arr1] # print the arr1 elements..

How do you print a grid in python?

Make it a function.
Write a function print_grid[n] that takes one integer argument and prints a grid just like before, BUT the size of the grid is given by the argument..
For example, print_grid[9] prints the grid at the top of this page..
print_grid[3] would print a smaller grid:.
print_grid[15] prints a larger grid:.

How do you print a two

To print out the entire two dimensional array we can use python for loop as shown below. We use end of line to print out the values in different rows.

Chủ Đề