muon
muon

Reputation: 14037

Convert each item of list of tuples to string

How to convert

[('a',1),('b',3)]

to

[('a','1'),('b','3')]

My end goal is to get:

['a 1','b 3']

I tried:

[' '.join(col).strip() for col in [('a',1),('b',3)]]

and

[' '.join(str(col)).strip() for col in [('a',1),('b',3)]]

Upvotes: 1

Views: 209

Answers (2)

Ulrich Schwarz
Ulrich Schwarz

Reputation: 7727

If you want to avoid the list comprehensions in jme's answer:

mylist = [('a',1),('b',3)]
map(lambda xs: ' '.join(map(str, xs)), mylist)

Upvotes: 2

jme
jme

Reputation: 20695

This ought to do it:

>>> x = [('a',1),('b',3)]
>>> [' '.join(str(y) for y in pair) for pair in x]
['a 1', 'b 3']

Upvotes: 4

Related Questions