How to do print array in Python?

Python

In this post, we are going to explore different ways of How to do print array in Python with code examples that include print array line by line in Python, print formatted array, and many more.

1. Print Python Formatted array


In this example, we will learn how to print the formatted array in Python.

To print formatted array output in Python we are using list comprehension with enumerate() function to get the index and value of array elements. Printing the formatted array using the print() method.

Program to print formatted array Using List Comprehesion

array_1D = [15, 16, 17, 18]


formatted_array = ["%i: %s" % (index, val) for index, val in  enumerate(array_1D)]

print(formatted_array)


Output

['0: 15', '1: 16', '2: 17', '3: 18']

Print formatted array Using join()

The other way to print the array is by using the join() method with enumerate() function.

array_1D = [15, 16, 17, 18]
print('\n'.join('{}: {}'.format(*val) for val in enumerate(array_1D)))

Output

0: 15
1: 16
2: 17
3: 18

2. How to print array line by line


In this example, we will learn about how to print arrays line by line using different ways. In this example, we are using string.join() method to print an array line by line.

We will also learn, how to print arrays without brackets with the help of these examples.

These are the 3 ways to print an array line by line.

Program to print array using string.join()

array_1D = [15, 16, 17, 18]


print(printing array line by line' '.join(str(item) for item in array_1D))

Output

15 16 17 18

Print an array line by line Using Map()

array_1D = [15, 16, 17, 18]
print(' '.join(map(str, array_1D)))

Output

15 16 17 18

Print array line by line Using * unpack operator

In this example, we are printing an array line by line using the * unpack operator.

array_1D = [15, 16, 17, 18]


print(*array_1D)

Output

15 16 17 18

4. Using the Print() method


In this example we are using the print() method to print a one-dimensional and two-dimensional array.

array_1D = [15, 16,17,18]

array_2D = [[3,5],[6,7],[8,9]]


print('printing 1D array :',array_1D)
print('printing 2D array:',array_2D)

Output

printing 1D array : [15, 16, 17, 18]
printing 2D array: [[3, 5], [6, 7], [8, 9]]

5. How to print array in Python Using for loop?


In this example, we are printing the 1D(array_1D) and 2D(array_2D) by Using for a loop. Iterating over each element of the array and printing the result using the print() method.

Program

array_1D = [15, 16, 17, 18]
array_2D = [[3, 5], [6, 7]]


print(f"Printing ID array using for loop:\n")
for element in array_1D:
    print(element, end = ' ')
 
print()


print(f"\nPrinting 2D array using for loop:\n")
for element in array_2D:
    for nested_tem in element:
        print(nested_tem, end=" ")
    print()

Output

Printing ID array using for loop:

15 16 17 18 

Printing 2D array using for loop:

3 5 
6 7

6. How to Print array of object in Python


  • In this example we have created a class devenum and initialized the members name and age.
  • We have created a empty list (mylist) and initializing values to members of class and appending to list.
  • Looping over each elements of list using the for loop and printing result using print() method

Program to Print array of object in Python

class devenum: 
    def __init__(self, name, age): 
        self.name = name 
        self.age = age
   
       
mylist = [] 
  


mylist.append( devenum('Jack', 21) )
mylist.append( devenum('Rack', 28) )
mylist.append( devenum('Ramo', 30) )
  
for obj in mylist:
    print( obj.name, obj.age, sep =' ' )

Output

Jack 21
Rack 28
Ramo 30

7. How to print Numpy Array?


In this example, we are converting the list to Numpy 1D and 2D array and printing the array using the print() method to print the NumPy array.

Program How to print numpy Array

import numpy as np

array_1D = np.array([15, 16,17,18])



array_2D = np.array([[3,5],[6,7],[8,9]])



print('Printing 1D NumPy array:\n',array_1D)

print('\nPrinting 2D NumPy array :',array_2D)

Output

Printing 1D NumPy array:
 [15 16 17 18]

Printing 2D NumPy array : [[3 5]
 [6 7]
 [8 9]]

8. How to print Python Numpy Array Using loop?


In this example, we will learn how to print Numpy 1D and 2D Array Using for loop.

Program To Print Python Numpy Array Using loop

import numpy as np

array_1D = np.array([15, 16,17,18])



array_2D = np.array([[3,5],[6,7],[8,9]])



print(f"Printing ID array using for loop:\n")
for element in array_1D:
    print(element, end = ' ')
 
print()
print(f"\nPrinting 2D array using for loop:\n")
for element in array_2D:
    for nested_tem in element:
        print(nested_tem, end=" ")
    print()

Output

Printing ID array using for loop:

15 16 17 18 

Printing 2D array using for loop:

3 5 
6 7 
8 9 

Summary

In this post, we have learned How to do print array in Python that includes Print Python Formatted array, How to print array line by line, How to print array in Python Using for loop, How to Print array of object in Python, How to print numpy Array, How to print numpy Array using a loop.