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.

Related Posts

  • Inverse matrix in Python
    24 August

    Inverse matrix in python

    Table of ContentsUsing the Gauss-Jordan method to find the inverse of a given matrix in Python.Using the numpy.linalg.inv() function to find the inverse of a given matrix in Python. The inverse of a matrix in simple mathematics can be defined as a matrix, which when multiplied by the original matrix, gives out the Identity matrix. […]

  • Convert list to matrix in Python
    21 August

    Convert list to matrix in Python

    Table of ContentsUsing the while loop to convert list to matrix in Python.Use the numpy.array() method to convert list to matrix in Python.Using the numpy.asarray() method to convert list to matrix in Python. Python provides some in-built data types to stock multiple items in a single variable. A List is one among these built-in data […]

  • Rotate image Python
    20 August

    Rotate image in Python

    Table of ContentsUsing the image.rotate() function from the Pillow Imaging Library (Fork) to rotate image in Python.Using NumPy library functions to rotate image in Python.Using the OpenCV and imutils library functions to rotate image in Python.Using matplotlib and SciPy libraries to rotate image in Python. Python supports the use of images and sometimes a given […]

  • 06 April

    Print Matrix in Python

    In this article you will learn "How to print a matrix in Python?"

  • 20 November

    How to initialize array in Python

    Table of ContentsUsing for loop, range() function and append() method of listIntialize empty arrayIntialize array with default valuesIntialize array with valuesUsing list-comprehensionUsing product (*) operatorUsing empty() method of numpy module In this article, we will see a different ways to initialize an array in Python. Array is collection of elements of same type. In Python, […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.