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

Printing a NumPy Array Without Brackets in Python

  • 26 Dec, 2024
  • Com 0
Printing Numpy Arrays without brackets

Whether you want to improve the readability of simple arrays or formatting for output to files or systems, sometimes you must present the array without brackets.

Here are two ways to print a Numpy array without brackets in Python:

  1. Using join(), map(), and str functions
  2. Using numpy.savetxt()

Method 1: Using join(), map(), and str functions

This approach requires conversion from a Numpy array to a string using map() and str. After converting it into a string, we can join the elements of a string with the desired separator using the join() function, which will print in the console.

Printing 1D numpy array

Printing 1D numpy array without brackets

import numpy as np

arr = np.array([19, 21, 0, 1, 2])

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

# 19 21 0 1 2

Printing 2D numpy array

Printing 2D numpy array

2D array is complex in structure and requires special attention. First, we must flatten the 2D array into a 1D array using the .flatten() method, and as we have done in the 1D array, use the join(), map(), and str functions.

import numpy as np

arr = np.array([[19, 21], [1, 2]])

print(" ".join(map(str, arr.flatten())))

# 19 21 1 2

If you want to print your array elements row-wise, you can do it using the code below:

Printing 2D array elements row-wise

import numpy as np

arr = np.array([[19, 21], [1, 2]])

for row in arr:
    print(" ".join(map(str, row)))

# 19  21
#  1   2

Pros

  1. Extremely lightweight and efficient for small to moderate array sizes.
  2. It works well with native Python types such as string and list.

Cons

  1. It lacks output formatting. Does not give much options.
  2. It becomes less performant when the dataset is large.
  3. Requires manual handling for multidimensional arrays.

Method 2: Using numpy.savetxt()

The numpy.savetxt() function saves an array to a text file, but we can use it for advanced formatting and output in the console.

Printing 1D array

Printing 1D array without bracket using np.savetxt()

import numpy as np
import sys

arr = np.array([1, 2, 3, 4, 5])
np.savetxt(sys.stdout, arr.reshape(1, -1), fmt='%d', delimiter=' ')

# 1 2 3 4 5

Printing 2D array

Printing 2D array without bracket using np.savetxt()

import numpy as np
import sys

arr = np.array([[19, 21], [1, 2]])
np.savetxt(sys.stdout, arr, fmt='%d', delimiter=' ')

# 19  21
#  1   2

Pros

  1. Since np.savetxt() is a numpy method, it is optimized for large datasets, and it is highly efficient in complex tasks.
  2. It supports advanced formatting and delimiter control.
  3. It works excellently for tasks requiring file output.

Cons

  1. If you are working with small data, the np.savetxt() method is not recommended.
  2. The syntax is slightly more complicated than the join() method.

That’s all!

Post Views: 177
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.

Printing Numpy Objects Without Line Breaks in Python
How to Convert a Numpy Array to String 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