Anonymous
Anonymous

Reputation: 477

Join a list that is full with tuples and not string

I wish to join a list of tuples to form a string.

l = [(2,3), (3,5), (4,5)]
print(" ".join(l))

My error:

TypeError: sequence item 0: expected str instance, tuple found

My expectation:

(2,3) (3,5) (4,5) #I only know how to join for string and I thought it will be simple until I found this. Please kindly assist me.

Upvotes: 0

Views: 103

Answers (2)

shaik moeed
shaik moeed

Reputation: 5785

Try this,

>>> l = [(2,3), (3,5), (4,5)]
>>> " ".join([str(i) for i in l])
'(2, 3) (3, 5) (4, 5)'

Upvotes: 1

Adam.Er8
Adam.Er8

Reputation: 13393

you need to cast the tuples to a string, you can do it by using map:

l = [(2, 3), (3, 5), (4, 5)]
print(" ".join(map(str, l)))

Upvotes: 1

Related Questions