Python list comprehension to join list of lists

Question:

Given lists = [['hello'], ['world', 'foo', 'bar']]

How do I transform that into a single list of strings?

combinedLists = ['hello', 'world', 'foo', 'bar']

Asked By: congusbongus

||

Answers:

from itertools import chain

combined = [['hello'], ['world', 'foo', 'bar']]
single = [i for i in chain.from_iterable(combined)]
Answered By: akira
lists = [['hello'], ['world', 'foo', 'bar']]
combined = [item for sublist in lists for item in sublist]

Or:

import itertools

lists = [['hello'], ['world', 'foo', 'bar']]
combined = list(itertools.chain.from_iterable(lists))
Answered By: Nicolas
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.