10

In Python how do I convert:

list01 = [ ['a', 'b', 'c'], ['i', 'j', 'k'], ['x', 'y', 'z'] ]

into

list02 = [ 'abc', 'ijk', 'xyz']

4 Answers 4

21

Using map:

map(''.join, list01)

Or with a list comprehension:

[''.join(x) for x in list01]

Both output:

['abc', 'ijk', 'xyz']

Note that in Python 3, map returns a map object instead of a list. If you do need a list, you can wrap it in list(map(...)), but at that point a list comprehension is more clear.

Sign up to request clarification or add additional context in comments.

Comments

3

Use str.join and a list comprehension:

>>> list01 = [ ['a', 'b', 'c'], ['i', 'j', 'k'], ['x', 'y', 'z'] ]
>>> [''.join(x) for x in list01]
['abc', 'ijk', 'xyz']
>>>

Comments

2
>>> map(''.join, list01)
['abc', 'ijk', 'xyz']

Comments

1

You can use join to implode the elements in a string and then append, if you don't want to use map.

# Your list
someList = [ ['a', 'b', 'c'], ['i', 'j', 'k'], ['x', 'y', 'z'] ]
implodeList = []

# make an iteration with for in
for item in someList:
    implodeList.append(''.join(item))

# printing your new list
print(implodeList)
['abc', 'ijk', 'xyz']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.