frazman
frazman

Reputation: 33213

merge all the elements in a list python

I have a list of string like:

s = [("abc","bcd","cde"),("123","3r4","32f")]

Now I want to convert this to the following:

output = ["abcbcdcde","1233r432f"]

What is the pythonic way to do this? THanks

Upvotes: 2

Views: 10965

Answers (5)

jamylak
jamylak

Reputation: 133504

>>> map(''.join, s)
['abcbcdcde', '1233r432f']

That should do it

Upvotes: 7

Artsiom Rudzenka
Artsiom Rudzenka

Reputation: 29093

Not a real answer, just want to check, what about reduce and operator.add, i read that in such a way both of them would perform quite fast and effective, or i am wrong?

s = [("abc","bcd","cde"),("123","3r4","32f")]

from operator import add

[reduce(add, x) for x in s]

Upvotes: 0

Wil Cooley
Wil Cooley

Reputation: 940

How about this:

>>> map(lambda x: ''.join(x), s)
['abcbcdcde', '1233r432f']

Upvotes: 0

Jonathon Reinhart
Jonathon Reinhart

Reputation: 137398

output = []
for grp in s:
    output.append(''.join(grp))

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798456

>>> [''.join(x) for x in s]
['abcbcdcde', '1233r432f']

Upvotes: 18

Related Questions