user4602735
user4602735

Reputation:

How to print a list without brackets

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

Upvotes: 1

Views: 13576

Answers (4)

Luis Alves
Luis Alves

Reputation: 194

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

Upvotes: 3

citaret
citaret

Reputation: 446

Just to print:

print(str(Floatlist).strip('[]'))
#out: 14.71525893389, 10.215953824, 14.8171645397, 10.2458542714719

Upvotes: 1

mgilson
mgilson

Reputation: 309929

.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.

Upvotes: 3

Daniel Roseman
Daniel Roseman

Reputation: 599610

You need to convert the elements to strings.

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

or

print (", ".join(str(f) for f in Floatlist)))

Upvotes: 5

Related Questions