How to Print an Array in Python

In this tutorial, you will learn how to print an array in Python.

Arrays are collections of data elements of the same type with the same name. Arrays can be implemented in Python using lists or the NumPy module. The NumPy module provides us with arrays of type ndarray (NumPy Array).

An array can also be multi-dimensional. Two-dimensional arrays are, as we all know, the most basic form of multi-dimensional arrays. As a result, in this tutorial, we will look at both 1D and 2D Arrays.

Methods To Print an Array in Python

Let’s take a look at some of the methods for printing both 1D and 2D arrays in Python. It should be noted that these arrays will be implemented using lists.

Printing Directly With The print() Method

To print the values from an array(list), we can directly pass the name of the array(list) to Python’s print() method.

However, in this case, the array is printed as a list, with brackets and values separated by commas.

#Initializing the Array
array1D = [1,2,3]
array2D = [[4,5,6], [7,8,9]]

#Printing The Array Directly Using Print
print(array1D)
print(array2D)

Output:

[1, 2, 3]
[[4, 5, 6], [7, 8, 9]]

array1D is a one-dimensional array in this case. array2D, on the other hand, is a two-dimensional one. We directly pass their names to the print() method, which prints them as a list and a list of lists, respectively.

Using For Loop In Python

In Python, we can also print an array by traversing all of its elements with for loops. Let us see in the below example code.

#Initializing the Array
array1D = [1,2,3]
array2D = [[4,5,6], [7,8,9]]

#Printing The Array Directly Using Print
print("Printing 1D Array Using For Loop: ")
for item in array1D:
    print(item, end=' ')

print("\nPrinting 2D Array Using For Loop:")
for givenList in array2D:
    for item in givenList:
        print(item, end=' ')

Output:

Printing 1D Array Using For Loop: 
1 2 3 
Printing 2D Array Using For Loop:
4 5 6 7 8 9 

Using for loops, we traverse the elements of a 1D and a 2D Array and print the corresponding elements in our desired form in the code above.

How To Print NumPy Array In Python

As previously stated, arrays can also be implemented in Python using the NumPy module. The module includes a pre-defined array class that can store values of the same type.

These NumPy arrays can be multidimensional as well. So, let’s see how we can print both 1D and 2D NumPy arrays in Python.

Using Simple Print Method

To print the arrays, we can directly pass the NumPy array name to the print() method, as we did with arrays implemented using lists.

#importing the NumPy Library
import numpy as np
 
#Initializing NumPy Array
array2D = np.array([[5,6],[7,8,9]])
array1D = np.array([1,2,3,4])

#printing the 1d numpy array
print("Numpy 1D array is: ", array1D) 

#printing the 2d numpy array
print("Numpy 2D-array is: ", array2D) 

Output:

Numpy 1D array is:  [1 2 3 4]
Numpy 2D-array is:  [list([5, 6]) list([7, 8, 9])]

An array1D and array2D are NumPy arrays of 1D and 2D dimensions, respectively. We pass their names to the print() method, which prints them both. It’s worth noting that the arrays are still printed in the form of NumPy arrays with brackets this time.

Using For Loop

Again, we can use loop structures to traverse NumPy arrays in Python. As a result, we can access and print each element of the array. This is yet another method for printing an array in Python.

Take a close look at the example below.

#importing the NumPy Library
import numpy as np
 
#Initializing NumPy Array
array2D = np.array([[5,6],[7,8,9]])
array1D = np.array([1,2,3,4])

#printing the 1d numpy array
print("Numpy 1D array is: ")
for item in array1D:
    print(item, end=' ') 

#printing the 2d numpy array
print("\nNumpy 2D-array is: ")
for preList in array2D:
    for item in preList:
        print(item, end=' ')
Numpy 1D array is: 
1 2 3 4
Numpy 2D-array is: 
5 6 7 8 9

We print the NumPy array elements in the desired manner (without brackets) here as well by accessing the elements of the 1D and 2D arrays individually.

How to Print an Array in Python

Wrap Up

So we learned how to print an array in Python in this tutorial. I hope you now have a firm grasp on the subject. Please use the comments section if you have any further questions about the topic.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. Remove a Character From String In Python
  2. How To Append Or Add String In Python
  3. How To Print A New Line In Python
  4. How To Downgrade Python

Leave a Comment