Print Array in Python

In this post, we will see how to print array in Python.
As we know that, Python didn’t have an in-built array data type, so we try to use list data type as an array. We can also use the NumPy module for creating NumPy array and apply array operation on it.

Now, we will first see how to print the list as an array, and then we will see how to print NumPy array in Python.

Print List

Using print()

Here, print() function is used to print the whole array along with [].

Below is the Python code given:

Output:

As you can see here, arr is one dimension array and you just need to pass it to print() method.

Similiarly, arr2d is two dimensional array and represents list of lists. You need to pass it to print() method to print 2D array in Python.

Using map()

If you directly use print() method to print the elements, then it will printed with [].

In case, you want to print elements in desired way, you can use map() method with join() method.

map() method will convert each item to string and then use join() method to join the strings with delimeter.

Here is an example

Below is the Python code given:

Output:

By unpacking list

You can use *list to print list of elements without brackets in Python 3.X.

*list simply unpacks the list and pass it to print function.
Below is the Python code given:

Output:

If you are using Python 2.x then you can import print function as below:

Using loop

Here, for loop is used to iterate through every element of an array, then print that element. We can also use while loop in place of for loop.

Below is the Python code given:

Output:

Here, we traversed to one dimenstional array arr and two dimensional array arr2d and print its elements.

Print Numpy-Array

Using print()

Here, print() function is used to print the whole NumPy array along with [].

Below is the Python code given:

Output:

Here arr and arr2d are one dimensional numpy array and two dimensional numpy array respectively. You need to pass it to print() method to print the array.

Using loop

Here, for loop is used to iterate through every element of a NumPy-array then print that element. We can also use while loop in place of for loop.

Below is the Python code given:

Output:

That’s all about how to print Array in Python.

Was this post helpful?

Leave a Reply

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