Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Print a Numpy Array in Python

  • 19 Nov, 2025
  • Com 0
Printing a Numpy Array or Python List

For debugging, inspecting intermediate outputs, or displaying structured results, you need to print the numpy array or list.

Method 1: Using print()

Using print() function to print the list and numpy array in Python

The most effortless way to print an array or list in Python is to use the print() function and pass that array or list to it. It is the quickest way to debug the input structure.

import numpy as np

py_list = [1, 2, 3, 4, 5]

print(py_list)

# Output: [1, 2, 3, 4, 5]

numpy_array = np.array(py_list)

print(numpy_array)

# Output: [1 2 3 4 5]

In this code, we defined a simple list and printed it on the console.

We used the same list to create a numpy array by passing it to the np.array() method. Then, we printed it.

The main disadvantage of using just the print() method is that it does not work well with nested lists or arrays. However, it can print nested structures, but they are not easily readable.

Method 2: pprint.pprint()

The pprint.pprint() method pretty-prints nested/complex lists in a human-readable format. Import the built-in pprint library.

from pprint import pprint

nested_list = [{'name': 'Krunal', 'hobbies': [
    'watching', 'gym']}, 10, [11, [19, 21]]]

pprint(nested_list, width=40)

# Output:
# [{'hobbies': ['watching', 'gym'],
#   'name': 'Krunal'},
#  10,
#  [11, [19, 21]]]

As you can see from the above output, it is ideal for inspecting deeply nested lists.

The disadvantage of the pprint module is that it does not work with numpy arrays. It throws ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (3,) + inhomogeneous part.

Method 3: Printing elements one by one using a for loop

What if you want better control over spacing, styling, or separators between elements while printing? In that case, use a for loop and print elements one by one.

For a 1D list, the loop iterates through each element, printing them one by one.

For a 2D (multidimensional) list, a nested for loop is used: the outer loop iterates over each row, while the inner loop iterates over each element in that row, enabling the list to be printed in a matrix-like format.

List

even_list = [2, 4, 6, 8, 10]
even_2d_list = [[2, 4], [6, 8]]

for element in even_list:
    print(element)

# Output:
# 2
# 4
# 6
# 8
# 10

for row in even_2d_list:
    for x in row:
        print(x, end=" ")
    print()

# Output:
# 2  4
# 6  8

NumPy array

import numpy as np

odd_array = np.array([1, 3, 5, 7, 9])
odd_2d_array = np.array([[1, 3], [5, 7]])

for element in odd_array:
    print(element)

# Output:
# 1
# 3
# 5
# 7
# 9


for row in odd_2d_array:
    for x in row:
        print(x, end=" ")
    print()

# Output:
# 1   3
# 5   7

Method 4: Unpacking elements (space-separated)

Unpacking elements (space-separated) using print(*)

If you want to print a list or array without brackets or commas, for a clean one-liner output, use the print(*list) function.

import numpy as np

list = [1, 2, 3, 4, 5]

print(*list)

# Output: 1 2 3 4 5

array = np.array(list)

print(*array)

# Output: 1 2 3 4 5

The above printed list and array outputs are readable without brackets or commas.

Method 5: Using join()

If you have a list of strings, you can use the .join() method with any separator you like and print the formatted list in the console.

list = ["bmw", "audi", "jaguar"]

print(", ".join(list))

# Output: bmw, audi, jaguar

It only works with a list of strings and not numeric values.

That’s all!

Post Views: 34
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Assign Variable Inside an Expression in Python
PIL Image.save(): Saving JPEG File in Python

Leave a Reply Cancel reply

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

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend