AppDividend
Latest Code Tutorials

How to Print an Array in Python

An array in programming is a collection of elements stored at contiguous memory locations. Array in Python can be created by importing an array module or numpy module. We will use the numpy module for this example because they are memory efficient.

Python print array

To print an array in Python, use the built-in print() function. The print() is an inbuilt Python function that takes the name of the array containing the values and prints it.

To create an array in Python, use numpy library and create an array using np.array() function and then print that array in the console.

import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)

Output

[1 2 3 4 5]

We have created a ndarray object by using the array() function. The print() function takes an arr as an argument and prints the array directly to the console.

The printed array is a One-dimensional array because we created a one-dimensional array.

Print two-dimensional array in Python

To print a two-dimensional array in Python, use the print() function. To create a 2D array in Python, use the np.array() function.

import numpy as np
arr = np.array([[1, 2], [3, 4], [5, 6]])
print(arr)

Output

[[1 2]
[3 4]
[5 6]]

And we printed the 2D Python array using the print() function.

Python print list

To print the list in Python, use the print() function. Python does not have a built-in Array data type, but it has a list that you can consider as an array in some cases. So let’s see how to print the list.

list = [11, 19, 21, 46]
print(list)

Output

[11, 19, 21, 46]

And that’s how you print the list and array in Python.

That’s it for this tutorial.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.