There is always the easy way.

import numpy as np
print(np.matrix(A))
Answer from Souradeep Nanda on Stack Overflow
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how do i pretty print a 2d array in python?
How do I pretty print a 2D array in Python? : r/learnpython
November 20, 2017 - If you do print(num) for every number in the array then of course every number is on a line all by itself. What you have to do is print every number on the same line as the previous number and only print a newline after every sub-list.
Discussions

python - How to print a 2D array in a "pretty" format? - Stack Overflow
Firstly, the way you are iterating through the array is fairly counter-intuitive. You can simply use ยท def printArray(arr): for row in arr: for item in row: # code for printing More on stackoverflow.com
๐ŸŒ stackoverflow.com
Print a 2D array in a certain way in Python - Stack Overflow
1 How to print all the elements of a 2d array in python with a different number of rows than columns? More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - How can I print a 2d array in different ways - Stack Overflow
I have got an array like this one a = [[1, 2, 3, 4],[5, 6, 7, 8]]. I want to be able to split it up so that it prints it out like the first item from the lists on one line and then the second items... More on stackoverflow.com
๐ŸŒ stackoverflow.com
January 8, 2020
how to print 2D array in python - Stack Overflow
I have a 2D array nxn, and I would like to print the content of it on the screen. board = { (i,j):"-" for i in range(n) for j in range(n) } #print(board) def display_board(): for row in board: for column in board[row]: print(board[row,column]) ... My code above gives me error. I am new to python, ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
People also ask

Are 2D arrays memory-efficient in Python?
2D arrays in Python, especially when created with standard lists, can consume more memory than necessary due to Python's dynamic typing. Using NumPy can help improve memory efficiency.
๐ŸŒ
upgrad.com
upgrad.com โ€บ home โ€บ tutorials โ€บ software & tech โ€บ 2d array in python
Comprehensive Guide to 2D Arrays in Python: Creation, Access, and Use
What are the challenges when working with 2D arrays?
Common challenges include handling boundary conditions, avoiding index errors, and efficiently traversing or searching through large 2D arrays.
๐ŸŒ
upgrad.com
upgrad.com โ€บ home โ€บ tutorials โ€บ software & tech โ€บ 2d array in python
Comprehensive Guide to 2D Arrays in Python: Creation, Access, and Use
๐ŸŒ
Snakify
snakify.org โ€บ two-dimensional lists (arrays)
Two-dimensional lists (arrays) - Learn Python 3 - Snakify
To process 2-dimensional array, you typically use nested loops. The first loop iterates through the row number, the second loop runs through the elements inside of a row. For example, that's how you display two-dimensional numerical list on ...
๐ŸŒ
Upgrad
upgrad.com โ€บ home โ€บ tutorials โ€บ software & tech โ€บ 2d array in python
Comprehensive Guide to 2D Arrays in Python: Creation, Access, and Use
November 11, 2024 - # Searching for an element in a ... == target: found = True break if found: print(f"{target} found in the 2D array.") else: print(f"{target} not found in the 2D array.") ... 2D arrays have a wide range of applications ...
Top answer
1 of 4
2

Here is what your are using:

def printArray(a):
    for row in range(len(a[0])):
        for col in range (len(a[0])):
            b = print("{:8.3f}".format(a[row][col]), end = " ")
        print(b)

You are using two for loops with len(a[0]) but your input data isn't a square, so that can't work!

You might consider using this:

def printA(a):
    for row in a:
        for col in row:
            print("{:8.3f}".format(col), end=" ")
        print("")

That will give you this:

In [14]: a = [[1, 2, 1, 2], [3, 4, 5, 3], [8, 9, 4, 3]]

In [15]: printA(a)
   1.000    2.000    1.000    2.000 
   3.000    4.000    5.000    3.000 
   8.000    9.000    4.000    3.000 

In [16]: b = [[1, 2, 1, 2, 5], [3, 4, 7, 5, 3], [8, 2, 9, 4, 3], [2, 8, 4, 7, 6]]

In [17]: printA(b)
   1.000    2.000    1.000    2.000    5.000 
   3.000    4.000    7.000    5.000    3.000 
   8.000    2.000    9.000    4.000    3.000 
   2.000    8.000    4.000    7.000    6.000 
2 of 4
1

There are three main issues with the code you have posted: the way of iterating through the array, the assignment of the b variable to a the return of a print statement, and the printing of that b variable.

Firstly, the way you are iterating through the array is fairly counter-intuitive. You can simply use

def printArray(arr):
    for row in arr:
        for item in row:
            # code for printing

to make it much more clear.

Secondly, your understanding of the print statement seems to be a bit lacking. The print statement takes in an argument and prints it out directly, so there is no need to assign it to a variable. Since the print statement has no official return, it automatically returns None, which ties in with the next point to explain the Nones at the end of your print statements.

Finally, the printing of the b variable which has a value None assigned to it as discussed above produces the Nones you see.


To fix your code, you could use the following solution.

a = [[1,2,1,2], [3,4,5,3], [8,9,4,3]]

def printArray(arr):
    for row in arr:
        for item in row:
            print("{:8.3f}".format(item), end = " ")
        print("")

printArray(a)

Other than the things stated above, this code differs by adding a print(""), which is equivalent to a new line, after every row in the array.

Find elsewhere
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 66445954
Print a 2D array in a certain way in Python - Stack Overflow
I generate this 2D array by appending arrays, for example the next one I add could be np.array([7, 8, 9]). Finally, I perform np.array() on the final 2D object a. I use np.array2string(a, separator=',') to print it, but then I get something ...
๐ŸŒ
AskPython
askpython.com โ€บ python โ€บ two-dimensional-array-in-python
Two Dimensional Array in Python - AskPython
August 6, 2022 - size = int(input()) array_input = [] for x in range(size): array_input.append([int(y) for y in input().split()]) print(array_input) ... Elements in a 2D array can be inserted using the insert() function specifying the index/position of the element ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ python_data_structure โ€บ python_2darray.htm
Python - 2-D Array
To print out the entire two dimensional array we can use python for loop as shown below.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 59644822
python - How can I print a 2d array in different ways - Stack Overflow
January 8, 2020 - File "/Users/owennichol/PycharmProjects/untitled2/AA.py", line 2, in <module> for i,j in zip(*a): print(i, j) ValueError: too many values to unpack How do I fix this. Thank you for the quick response. 2020-01-08T10:54:06.48Z+00:00 ... @OwenNichol If this is happening, list a must be different there in that file AA.py. 2020-01-08T10:55:30.003Z+00:00 ... a = [[1, 2, 3, 4],[5, 6, 7, 8]] output = '\n'.join([' '.join([str(values[i]) for values in a]) for i in range(len(a[0]))]) print (output) ... if yout sublist are different length you can use for Python 3 itertools.zip_longest() or itertools.izip_longest (Python 2.6+):
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 10929 โ€บ how-to-print-2d-array-in-python
how to print 2d array in python
June 29, 2016 - Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 60766018 โ€บ how-to-print-2d-array-in-python
how to print 2D array in python - Stack Overflow
I have a 2D array nxn, and I would like to print the content of it on the screen. board = { (i,j):"-" for i in range(n) for j in range(n) } #print(board) def display_board(): for row in board: for column in board[row]: print(board[row,column]) ... ...
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 2950044 โ€บ how-we-can-print-2d-array-without-brackets-commas-and-what-is-the-way-to-format-them
https://www.sololearn.com/en/Discuss/2950044/how-w...
Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.
๐ŸŒ
AskPython
askpython.com โ€บ python โ€บ array โ€บ print-an-array-in-python
How to Print Arrays in Python? - AskPython
2 weeks ago - To print arrays in Python, you can use the print() function directly for simple output or implement loops for formatted display. This guide covers both approaches for 1D and 2D arrays with practical examples.
๐ŸŒ
Cf
physics-python.astro.cf.ac.uk โ€บ Week6
PX1224 -Week6: Two Dimensional Arrays
c = d2[ : :2 , : :2] print(c) Note:In the above example only the step size of 2 is specified - the start and the end rows/columns are not specified, in which case they are assumed to be the first and last column of the 2d array. A 2D array can be "sliced" to return a 1D array containing one ...
๐ŸŒ
Javatpoint
javatpoint.com โ€บ python-2d-array
Python 2D array - Javatpoint
January 10, 2021 - Python 2D array with python, tutorial, tkinter, button, overview, entry, checkbutton, canvas, frame, environment set-up, first python program, operators, etc.
๐ŸŒ
Google
sites.google.com โ€บ rgc.aberdeen.sch.uk โ€บ rgcahcomputingrevision โ€บ software-design โ€บ data-types-and-structures โ€บ 2d-arrays
Advanced Higher Computing Revision - 2D Arrays
Python treats this as a list within a list. You will notice that we have initialised each element in the array with a value of 0. This would create a 2D array called my2dArray with 3 rows and 8 columns.