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,995
  • 2
  • 23
  • 38

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
  • 588,541
  • 66
  • 880
  • 895
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
  • 300,191
  • 65
  • 633
  • 696
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
  • 416
  • 4
  • 11