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 or numpy module. We will use the numpy module because they are memory efficient.

Python print array

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

To create an array in Python, use the numpy library, create an array using the np.array() function, and 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 created a ndarray object using the np.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 a 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 you can consider 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.

Related posts

Print bold text in Python

Python print no newline

How to print type of variable in Python

Leave a Comment

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