Python

Suppress Scientific Notation in Numpy When Creating Array From Nested List

19 September 2026 · 10 min read

Suppress Scientific Notation in Numpy When Creating Array From Nested List

Working with numerical data in Python often involves using NumPy, a powerful library for array manipulation. However, when creating arrays from nested lists, especially those containing very large or very small numbers, NumPy may default to displaying these numbers in scientific notation. While this can be convenient in some situations, it’s often preferable to suppress scientific notation in NumPy, particularly when presenting data in a report or visualizing results. This article provides a comprehensive guide on how to control NumPy’s output formatting to display numbers in a more readable, fixed-point format. We will explore different methods to achieve this, catering to various scenarios and preferences, ensuring your data is presented clearly and effectively. Whether you are dealing with financial data, scientific measurements, or any other numerical datasets, mastering the art of formatting NumPy arrays is essential for effective data analysis and communication.

Understanding NumPy’s Default Formatting

NumPy, by default, attempts to represent numerical data in a way that balances precision and readability. This often leads to the use of scientific notation (e.g., 1.234e+05) for numbers that are either very large or very small. This notation is compact and avoids displaying a large number of digits, but it can sometimes obscure the true value of the number, especially for those unfamiliar with scientific notation. Understanding why NumPy defaults to this behavior is crucial before diving into how to change it. The decision is often based on the data type and the range of values within the array. For instance, floating-point numbers, due to their inherent representation in computers, are more prone to being displayed in scientific notation than integers.

Furthermore, NumPy’s printing options are global, which means that changes you make will affect how all NumPy arrays are displayed in your current session. This is important to keep in mind, as you might want to revert to the default settings after you are done with a specific task that requires suppressing scientific notation. Different operating systems and environments may also have slight variations in how NumPy’s default formatting behaves, so it’s always good to be aware of these potential differences. A good understanding of these underlying mechanisms is essential for effectively controlling NumPy’s output.

According to the NumPy documentation, the default behavior is designed to provide a balance between precision and visual clarity. However, many users prefer to have more control over the output format, especially when dealing with specific types of data or when presenting results to a non-technical audience. This control allows for better clarity and easier interpretation of the data. By understanding the default formatting, we can better appreciate the various methods available to customize NumPy’s output.

Methods to Suppress Scientific Notation

There are several ways to suppress scientific notation in NumPy when creating arrays from nested lists. Each method has its advantages and disadvantages, depending on the specific context and desired outcome. One of the most common approaches involves using np.set_printoptions, a function that allows you to customize various printing options, including the suppression of scientific notation. This method provides a global setting that affects all subsequent NumPy array print outputs. Another approach is to format individual elements of the array using string formatting, which offers more granular control but can be more verbose.

Another method involves changing the data type of the array to a string representation, which effectively prevents NumPy from interpreting the numbers as numerical values and thus avoids scientific notation. However, this approach comes with the caveat that you can no longer perform numerical operations on the array directly. Finally, some visualization libraries, such as Matplotlib, offer their own formatting options that can override NumPy’s default settings when displaying arrays in plots or charts. The best approach will depend on the specific use case, whether you need to perform further calculations on the array, or if you are simply presenting the data in a human-readable format.

The featured snippet-optimized paragraph: One of the most straightforward methods to suppress scientific notation in NumPy is by using the np.set_printoptions function. Specifically, you can set the suppress parameter to True. For example, np.set_printoptions(suppress=True) will globally disable scientific notation for all subsequent NumPy array print outputs. This ensures that all numbers are displayed in a fixed-point format, making the data easier to read and understand.

  • np.set_printoptions(suppress=True): Globally disables scientific notation.
  • String formatting: Provides granular control over individual elements.

Practical Examples and Code Snippets

Let’s illustrate these methods with practical examples and code snippets. Suppose you have a nested list containing large numbers that NumPy displays in scientific notation by default. You can use np.set_printoptions(suppress=True) to change this behavior. Here’s a simple example:

import numpy as np Create a NumPy array from a nested list data = [[1000000000, 0.000000001], [2000000000, 0.000000002]] arr = np.array(data) print("Default NumPy Output:\n", arr) Suppress scientific notation np.set_printoptions(suppress=True) print("\nSuppressed Scientific Notation:\n", arr) Restore default settings np.set_printoptions(suppress=False) print("\nRestored Default Output:\n", arr) 

This code snippet demonstrates how to globally suppress scientific notation in NumPy. You can also use string formatting to format individual elements or rows of the array. For example, you can use the %.3f format specifier to display numbers with three decimal places. This approach is particularly useful when you need to present numbers in a specific format for reporting or visualization purposes. Another option is to convert the entire array to a string array, which prevents NumPy from using scientific notation, but also prevents you from performing numerical operations on the array.

Here’s an example using string formatting:

import numpy as np data = [[1000000000, 0.000000001], [2000000000, 0.000000002]] arr = np.array(data) Format each element as a string with 3 decimal places formatted_arr = np.array([["%.3f" % x for x in row] for row in arr]) print("\nFormatted Array:\n", formatted_arr) 

Remember to choose the method that best suits your needs, considering the trade-offs between readability, precision, and the ability to perform numerical operations.

Advanced Formatting Options

Beyond simply suppressing scientific notation, NumPy offers a range of advanced formatting options to customize the display of arrays. These options allow you to control the number of decimal places, the width of the output, and the use of a separator between digits. By combining these options, you can create highly tailored output formats that meet the specific requirements of your data and presentation style. For example, you might want to display numbers with a fixed number of decimal places, regardless of their magnitude, or you might want to align numbers in a column to improve readability. These advanced options can be particularly useful when working with large datasets or when presenting data in a professional report.

One powerful option is the precision parameter in np.set_printoptions, which allows you to specify the number of digits to display after the decimal point. Another useful parameter is linewidth, which controls the maximum number of characters to display per line. You can also use the formatter parameter to define custom formatting functions for individual elements of the array. These advanced options provide a high degree of flexibility in controlling the appearance of NumPy arrays. Experimenting with these options can significantly improve the readability and clarity of your data.

For instance, you can combine suppress and precision to display numbers with a fixed number of decimal places without using scientific notation:

import numpy as np data = [[1000.123456, 0.000123456], [2000.654321, 0.000654321]] arr = np.array(data) np.set_printoptions(suppress=True, precision=3) print("\nAdvanced Formatting:\n", arr) 

This code snippet shows how to suppress scientific notation and set the precision to three decimal places, resulting in a clean and readable output.

  1. Import the NumPy library: import numpy as np
  2. Create your NumPy array: arr = np.array(data)
  3. Use np.set_printoptions(suppress=True) to suppress scientific notation.
  4. Print the array to see the formatted output: print(arr)
  5. If needed, revert to default settings with np.set_printoptions(suppress=False)
Infographic here
FAQ: Common Questions About NumPy Formatting --------------------------------------------
How do I permanently **suppress scientific notation in NumPy**?
You can set `np.set_printoptions(suppress=True)` at the beginning of your script or interactive session. However, this setting is only valid for the current session. To make it permanent, you would need to add this line to your NumPy configuration file or set it in your environment variables. This is generally not recommended, as it can affect other scripts or programs that rely on the default formatting.
Can I format specific elements of a NumPy array differently?
Yes, you can use string formatting or custom formatting functions with the `formatter` parameter in `np.set_printoptions` to format specific elements differently. This allows for fine-grained control over the output format.
Why is NumPy still using scientific notation even after I set `suppress=True`?
Ensure you are running the `np.set_printoptions(suppress=True)` command before printing the array. Also, check if any other settings or libraries are overriding NumPy's formatting options. Sometimes, visualization libraries like Matplotlib can interfere with NumPy's printing settings.
- Controlling Decimal Places - Aligning Numbers in a Column

Mastering NumPy’s formatting options is crucial for presenting data in a clear and understandable manner. By understanding the various methods available and experimenting with different settings, you can tailor the output to meet your specific needs and ensure that your data is presented effectively. Remember that the best approach will depend on the specific context and the desired outcome, so it’s important to consider the trade-offs between readability, precision, and the ability to perform numerical operations. Libraries like Pandas rely on NumPy, so formatting in NumPy will affect Pandas DataFrames too. To learn more about customizing NumPy’s output, refer to the official NumPy documentation [external link to NumPy documentation](https://numpy.org/doc/stable/reference/generated/numpy.set_printoptions.html). For practical examples, check out Stack Overflow discussions on NumPy formatting [external link to Stack Overflow](https://stackoverflow.com/questions/2891790/how-to-pretty-print-a-numpy-array-without-scientific-notation-and-with-given-pre) and relevant blog posts on data visualization [external link to a data visualization blog](https://www.dataquest.io/blog/numpy-tutorial-python/). For additional control, explore other formatting libraries in Python, such as the decimal module. Using these techniques, you can improve readability and analysis capabilities.

By understanding and applying the techniques discussed, you can ensure that your numerical data is presented clearly and effectively, improving your data analysis workflow and communication. Experiment with the different methods, and choose the ones that best suit your specific needs. Whether you’re working with scientific data, financial models, or any other numerical datasets, mastering NumPy’s formatting options will empower you to present your results with confidence and clarity. Consider diving deeper into other NumPy functionalities like array manipulation and broadcasting to enhance your data processing skills.

Question & Answer :
I have a nested Python list that looks like the following:

my_list = [[3.74, 5162, 13683628846.64, 12783387559.86, 1.81], [9.55, 116, 189688622.37, 260332262.0, 1.97], [2.2, 768, 6004865.13, 5759960.98, 1.21], [3.74, 4062, 3263822121.39, 3066869087.9, 1.93], [1.91, 474, 44555062.72, 44555062.72, 0.41], [5.8, 5006, 8254968918.1, 7446788272.74, 3.25], [4.5, 7887, 30078971595.46, 27814989471.31, 2.18], [7.03, 116, 66252511.46, 81109291.0, 1.56], [6.52, 116, 47674230.76, 57686991.0, 1.43], [1.85, 623, 3002631.96, 2899484.08, 0.64], [13.76, 1227, 1737874137.5, 1446511574.32, 4.32], [13.76, 1227, 1737874137.5, 1446511574.32, 4.32]] 

I then import Numpy, and set print options to (suppress=True). When I create an array:

my_array = numpy.array(my_list) 

I can’t for the life of me suppress scientific notation:

[[ 3.74000000e+00 5.16200000e+03 1.36836288e+10 1.27833876e+10 1.81000000e+00] [ 9.55000000e+00 1.16000000e+02 1.89688622e+08 2.60332262e+08 1.97000000e+00] [ 2.20000000e+00 7.68000000e+02 6.00486513e+06 5.75996098e+06 1.21000000e+00] [ 3.74000000e+00 4.06200000e+03 3.26382212e+09 3.06686909e+09 1.93000000e+00] [ 1.91000000e+00 4.74000000e+02 4.45550627e+07 4.45550627e+07 4.10000000e-01] [ 5.80000000e+00 5.00600000e+03 8.25496892e+09 7.44678827e+09 3.25000000e+00] [ 4.50000000e+00 7.88700000e+03 3.00789716e+10 2.78149895e+10 2.18000000e+00] [ 7.03000000e+00 1.16000000e+02 6.62525115e+07 8.11092910e+07 1.56000000e+00] [ 6.52000000e+00 1.16000000e+02 4.76742308e+07 5.76869910e+07 1.43000000e+00] [ 1.85000000e+00 6.23000000e+02 3.00263196e+06 2.89948408e+06 6.40000000e-01] [ 1.37600000e+01 1.22700000e+03 1.73787414e+09 1.44651157e+09 4.32000000e+00] [ 1.37600000e+01 1.22700000e+03 1.73787414e+09 1.44651157e+09 4.32000000e+00]] 

If I create a simple numpy array directly:

new_array = numpy.array([1.5, 4.65, 7.845]) 

I have no problem and it prints as follows:

[ 1.5 4.65 7.845] 

Does anyone know what my problem is?

This is what you need:

np.set_printoptions(suppress=True) 

Here is the documentation which says

suppress: bool, optional

If True, always print floating point numbers using fixed point notation, in which case numbers equal to zero in the current precision will print as zero. If False, then scientific notation is used when absolute value of the smallest number is < 1e-4 or the ratio of the maximum absolute value to the minimum is > 1e3. The default is False.

In the original question, the difference between the array created “directly” and the original “big” array is that the big array contains very large numbers (e.g. 1.44651157e+09), so NumPy chooses the scientific notation for it, unless it’s suppressed.