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.
Table of Contents
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:
1 2 3 4 5 6 7 8 9 10 11 |
# 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:
1 2 3 4 |
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:
1 2 3 4 5 6 7 8 9 |
# integers list arr = [10, 20, 30, 40, 50, 60] print(' '.join(map(str, arr))) # In new line print('\n'.join(map(str, arr))) |
Output:
1 2 3 4 5 6 7 8 9 |
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
.
1 2 3 |
print(*list) |
*list
simply unpacks the list and pass it to print function.
Below is the Python code given:
1 2 3 4 5 6 7 8 9 |
# Print array in Python arr = [20, 40, 80, 100, 120] print(*arr) # Print array in new line print(*arr,sep='\n') |
Output:
1 2 3 4 5 6 7 8 |
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:
1 2 3 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# 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:
1 2 3 4 5 6 7 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# 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:
1 2 3 4 5 6 7 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# 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:
1 2 3 4 5 6 7 8 |
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.