1

I have a list with float values. I want to remove the the brackets from the list.

Floatlist = [14.715258933890,10.215953824,14.8171645397,10.2458542714719]
print (", ".join(Floatlist))

but i am getting an following error :
TypeError: sequence item 0: expected string, float found

but i want to print the list like:

output:14.715258933890,10.215953824,14.8171645397,10.2458542714719
benten
  • 1,935
  • 1
  • 22
  • 37

4 Answers4

5

You need to convert the elements to strings.

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

or

print (", ".join(str(f) for f in Floatlist)))
Daniel Roseman
  • 567,968
  • 59
  • 825
  • 842
3

.join only operates on iterables that yield strings. It doesn't convert to strings implicitly for you -- You need to do that yourself:

','.join([str(f) for f in FloatList])

','.join(str(f) for f in FloatList) also works (notice the missing square brackets), but on CPython, it's pretty well known that the version with the list comprehension performs slightly better.

mgilson
  • 283,004
  • 58
  • 591
  • 667
3

You just make a for statement:

for i in Floatlist:
    print(i, ', ', end='')

Hope that helps

P.S: This snippet code only works in Python3

Luis Alves
  • 194
  • 2
  • 10
1

Just to print:

print(str(Floatlist).strip('[]'))
#out: 14.71525893389, 10.215953824, 14.8171645397, 10.2458542714719
citaret
  • 396
  • 3
  • 11