Search code examples
pythonlistscientific-notation

Scientific notation in list form in Python


I have a list A. I want to convert the elements of this list to scientific notation. I present the current and expected output.

A = [0.000129,0.000148]
A_scientific = [f'{num:.2e}' for num in A]
print(A_scientific)

The current output is

['1.29e-04', '1.48e-04']

The expected output is

[1.29e-04, 1.48e-04]

Solution

  • You could use a str.join() within an f-string like this:

    A = [0.000129,0.000148]
    A_scientific = [f'{num:.2e}' for num in A]
    print(f"[{', '.join(A_scientific)}]")
    

    ...or by concatenation...

    print("[" + ", ".join(A_scientific) + "]")
    

    Output:

    [1.29e-04, 1.48e-04]