Python print array Code example

Last Updated On Thursday 14th Apr 2022

Python Print Array

Arrays are a collection of data elements of the same type under the same name. In Python, we can implement arrays using lists.
An array can be multi-dimensional. As we know, the simplest form of multi-dimensional arrays is two-dimensional arrays.

Using For Loop

We can also print an array in Python by traversing through all the respective elements using for loops.

	from array import *

arr = array('i', [1,2,3,4,5,6,7,8]) # describe the use of this code!

for x in arr: # describe the use of this code!
 print(x)
	

print an array python

Output

	1
2
3
4
5
6
7
8
	
  • One index referring to the primary or parent array.
  • Another index was referring to the position of the data element in the inner array.
  • If we mention only one index, then the entire inner array is printed for that index position.

show array python

	
from array import *

arr = [[1,2,3,4], [5,6,7,8], [9,10,11,12], [13,14,15,16]]

print(arr[0]) # describe the use of this code!

print(arr[1][2]) # describe the use of this code!
	

Outputs

	[1, 2, 3, 4]
7
	

How to Print Array Python

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

arr = [[1,2,3,4], [5,6,7,8], [9,10,11,12], [13,14,15,16]]

for r in arr: # describe the use of this code!
    for c in r: # describe the use of this code!
        print(c,end = " ")
    print()
	

Outputs

	1 2 3 4 
5 6 7 8 
9 10 11 12 
13 14 15 16 
	

So, We learned how to print an array in Python.Hope You Have a Clear understanding.

References