Home > Blockchain >  How do i print a list of float values without the list brackets?
How do i print a list of float values without the list brackets?

Time:10-18

I have a list of float values, that I want to print in a single line, without the list brackets and commas.

The length of the list could get quite long, so I can't just use a simple print statement like this:

print(mylist[0], mylist[1], mylist[2])

I'll use the following list as an example:

mylist = [0.005, 0.354, 0.645]

Current output:

[0.005, 0.354, 0.645]

Desired output:

0.005 0.354 0.645

Are there any methods I can use to create this efficiently and without using direct indexes?

CodePudding user response:

Simply unpack the list using *:

print(*mylist)

CodePudding user response:

try it:

>>> mylist = [0.005, 0.354, 0.645]
>>> list2 = [str(i) for i in mylist]  # ['0.005', ...]
>>> ' '.join(list2):
0.005 0.354 0.645
>>> 

CodePudding user response:

Use str.join:

mylist = [0.005, 0.354, 0.645]
stringified = " ".join(map(str, mylist))
print(stringified)
  • Related