How To Print An Array In Python

What we are going to see through this article is we will see in detail How To Print An Array In Python. Arrays in Python are data structures that can hold multiple identical values. Often, they are misinterpreted as lists of dependent rows.

Technically, the arrays in Python are different from these two. So let’s go ahead and see what the arrays in Python are and how to implement them.

Here, each and every element of data will display using two python in-built modules. Those are:

  • Print() method,
  • For Loops.
How To Print An Array In Python

How to Print an Array in Python using print() method

Now, We can use the print() method to print an array in the below program.

arr = [2,4,5,7,9]
arr_2d = [[1,2],[3,4]]
 
print("The Array is: ", arr) #printing the array
print("The 2D-Array is: ", arr_2d) #printing the 2D-Array

Output

This is the example output for your reference.

The Array is:  [2, 4, 5, 7, 9]
The 2D-Array is:  [[1, 2], [3, 4]]

How to Print an Array in Python using for loops

Below, We can use the for loops rather than the print() method to do a task in the below program.

arr = [2,4,5,7,9]
arr_2d = [[1,2],[3,4]]
 
#printing the array
print("The Array is : ")
for i in arr:
    print(i, end = ' ')
 
#printing the 2D-Array
print("\nThe 2D-Array is:")
for i in arr_2d:
    for j in i:
        print(j, end=" ")
    print()

Output

The print() function will help to print the output on the screen.

The Array is : 
2 4 5 7 9
The 2D-Array is:
1 2
3 4

How to Print NumPy Array in Python using print() method

Here, We can print the NumPy array using the print() method in python.

import numpy as np
 
arr_2d = np.array([[21,43],[22,55],[53,86]])
arr = np.array([1,2,3,4])
 
print("Numpy array is: ", arr) #printing the 1d numpy array
 
print("Numpy 2D-array is: ", arr_2d) #printing the 2d numpy array

Output

This is an example output for your reference.

Numpy array is:  [1 2 3 4]
Numpy 2D-array is:  [[21 43]
 [22 55]
 [53 86]]

How to Print NumPy Array in Python using for loops

Instead of using the print() method, We can use the for loops to print the NumPy array in the below source code.

import numpy as np
 
arr = np.array([11,22,33,44])
arr_2d = np.array([[90,20],[76,45],[44,87],[73,81]])
 
#printing the numpy array
print("The Numpy Array is : ")
for i in arr:
    print(i, end = ' ')
 
#printing the numpy 2D-Array
print("\nThe Numpy 2D-Array is:")
for i in arr_2d:
    for j in i:
        print(j, end=" ")
    print()

Read Also: How To Initialize An Array In Python

Output

As a result, The print() function is used to display the result on the screen.

The Numpy Array is : 
11 22 33 44
The Numpy 2D-Array is:
90 20
76 45
44 87
73 81

Final Thoughts

What we learned through this article is How To Print An Array In Python. Also if you have any doubts please leave a comment via the comment box. And we ask that you benefit everyone who shared this article with your friends.

Leave a Comment