python numpy – Java2Blog https://java2blog.com A blog on Java, Python and C++ programming languages Mon, 18 Oct 2021 18:02:30 +0000 en-US hourly 1 https://wordpress.org/?v=6.2.9 https://java2blog.com/wp-content/webpc-passthru.php?src=https://java2blog.com/wp-content/uploads/2022/09/cropped-ICON_LOGO_TRANSPARENT-32x32.png&nocache=1 python numpy – Java2Blog https://java2blog.com 32 32 Print Array in Python https://java2blog.com/print-array-python/?utm_source=rss&utm_medium=rss&utm_campaign=print-array-python https://java2blog.com/print-array-python/#respond Wed, 18 Nov 2020 18:07:01 +0000 https://java2blog.com/?p=10994 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:

# integers list
arr = [10, 20, 30, 40, 50, 60]

print("Array :",arr)

# list of lists
arr2d = [ [10, 20], [30, 40] ]

print("2 Dim - Array :", arr2d)

Output:

Array : [10, 20, 30, 40, 50, 60]
2 Dim - Array : [[10, 20], [30, 40]]

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:

# integers list
arr = [10, 20, 30, 40, 50, 60]

print(' '.join(map(str, arr))) 

# In new line
print('\n'.join(map(str, arr)))

Output:

10 20 30 40 50 60
10
20
30
40
50
60

By unpacking list

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

print(*list)

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

# Print array in Python
arr = [20, 40, 80, 100, 120]

print(*arr) 

# Print array in new line
print(*arr,sep='\n')

Output:

20 40 80 100 120
20
40
80
100
120

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

from __future__ import print_function

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:

# integers list
arr = [10, 20, 30, 40, 50, 60]

print("Array Elements are: ")
for ele in arr:

    # print in a single line
    print(ele, end = " ")

print()

# list of lists
arr2d = [ [10, 20], [30, 40] ]

print("Elements of 2 Dim-array are: ")
for item in arr2d:
    for ele in item:

        # print in a single line
        print(ele, end= " ")
    print()

Output:

Array Elements are: 
10 20 30 40 50 60 
Elements of 2 Dim-array are: 
10 20 
30 40

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:

# import numpy
import numpy

# integers array
arr = numpy.array([10, 20, 30, 40, 50, 60])

print("Array :",arr)

# Print 2D array
arr2d = numpy.array([[10, 20], [30, 40], [50, 60]])
print("2 Dim - Array :\n", arr2d)

Output:

Array : [10 20 30 40 50 60]
2 Dim - Array :
 [[10 20]
 [30 40]
 [50 60]]

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:

# import numpy
import numpy

# integers array
arr = numpy.array([10, 20, 30, 40, 50, 60])

print("Array Elements are: ")
for ele in arr:

    # print in a single line
    print(ele, end = " ")

print()

# 2 dim-array
arr2d = numpy.array([[10, 20], [30, 40], [50, 60]])

print("Elements of 2 Dim-array are: ")
for item in arr2d:
    for ele in item:

        # print in a single line
        print(ele, end= " ")
    print()

Output:

Array Elements are: 
10 20 30 40 50 60 
Elements of 2 Dim-array are: 
10 20 
30 40 
50 60

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

]]>
https://java2blog.com/print-array-python/feed/ 0