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?


import_contacts

You may also like:

Related Posts

  • 17 May

    Add Month to datetime in Python

    Table of ContentsWays to add month to datetime in PythonUsing the datetime.datetime constructor to add month to datetime in Python.Using the dateutil.relativedelta object to add month to datetime in PythonUsing the pandas.DateOffset() function to add month to datetime in PythonUsing the arrow.shift() function to add month to datetime in PythonConclusionWas this post helpful? In Python, […]

  • Create an array of 1 to 10 in Python
    03 May

    Create an Array of 1 to 10 in Python

    Table of ContentsIntroductionHow to create an array of 1 to 10 in Python?Using the range() function to create an array of 1 to 10 in Python.Using list comprehension along with the range() function to create an array of 1 to 10 in Python.Using a user-defined function to create an array of 1 to 10 in […]

  • Python append to array
    30 December

    How to Append to Array in Python

    Table of ContentsWhat are the Different Array Implementations in Python?Python ListsPython ArraysPython Numpy ArraysAppend Element to List in pythonAppend to array in PythonAppend to NumPy array in pythonConclusionWas this post helpful? In python, we have three implementations of the array data structure. In this article, we will discuss those array implementations. After that, see how […]

  • Python array size
    17 December

    Python array size: get size of array in Python

    Table of ContentsPython array sizeWays to calculate the size of array in PythonUsing the len() functionUsing the numpy.size() attributeUsing the numpy.shape() attributeConclusionWas this post helpful? In this article, we will see how to get size of array in Python. Python array size An array stores a collection of similar elements in a contiguous memory location. […]

  • Python float array to int
    11 November

    Convert float array to int array in Python

    Table of ContentsWhat are the types of arrays?Ways to convert float arrays to int in PythonUsing the numpy.asarray() functionUsing the numpy.astype() functionUsing the numpy.int_() functionUsing the numpy.round_() functionUsing the numpy.rint() functionUsing the numpy.floor() functionUsing the numpy.ceil() functionUsing the numpy.trunc() functionConclusionWas this post helpful? What are the types of arrays? An array is a common structure […]

  • Convert Bytearray to String in Python
    01 November

    Convert Bytearray to String in Python

    Table of ContentsHow to create a bytearray from a string?Using the str() Function to convert Bytearray to String in PythonUsing the decode() Function to convert Bytearray to String in PythonUsing the codecs Module to convert Bytearray to String in PythonConclusionWas this post helpful? Python supports different types of sequence objects to store data. One such […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.