Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages

How to Print an Array in Python

Print Arrays In Python

Introduction

In this tutorial, we are going to learn how to print an array in Python.

So before we get right into the topic, let us know a bit about Arrays in Python.

Python Arrays

Arrays are a collection of data elements of the same type under the same name. In Python, we can implement arrays using lists or the NumPy module. The NumPy module provides us with arrays of type ndarray(NumPy Array).

Further, an array can be multi-dimensional. As we know, the simplest form of multi-dimensional arrays is two-dimensional arrays. Hence, in this tutorial, we are going to be considering 1D as well as 2D Arrays.

Ways to Print an Array in Python

Now, let us look at some of the ways to print both 1D as well as 2D arrays in Python. Note: these arrays are going to be implemented using lists.

Directly printing using the print() method

We can directly pass the name of the array(list) containing the values to be printed to the print() method in Python to print the same.

But in this case, the array is printed in the form of a list i.e. with brackets and values separated by commas.

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:

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

Here, arr is a one-dimensional array. Whereas, arr_2d is a two-dimensional one. We directly pass their respective names to the print() method to print them in the form of a list and list of lists respectively.

Using for loops in Python

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

Let us see how.

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 Array is : 
2 4 5 7 9 
The 2D-Array is:
1 2 
3 4

In the code above we traverse through the elements of a 1D as well as a 2D Array using for loops and print the corresponding elements in our desired form.

Ways to print NumPy Array in Python

As mentioned earlier, we can also implement arrays in Python using the NumPy module. The module comes with a pre-defined array class that can hold values of same type.

These NumPy arrays can also be multi-dimensional. So, let us see how can we print both 1D as well as 2D NumPy arrays in Python.

Using print() method

Similar to the case of arrays implemented using lists, we can directly pass NumPy array name to the print() method to print the arrays.

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:

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

Here, arr and arr_2d are one 1D and one 2D NumPy arrays respectively. We pass their names to the print() method and print both of them. Note: this time also the arrays are printed in the form of NumPy arrays with brackets.

Using for loops

Again, we can also traverse through NumPy arrays in Python using loop structures. Doing so we can access each element of the array and print the same. This is another way to print an array in Python.

Look at the example below carefully.

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()

Output:

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

Here also we print the NumPy array elements in our desired way(without brackets) by accessing the elements of the 1D and 2D array individually.

Conclusion

So in this tutorial, we learned how to print an array in Python. I hope you now have a clear understanding of the topic. For any further question related to the topic, feel free to use the comments.

References

Leave a Reply

Your email address will not be published. Required fields are marked *