Python list comprehension to join list of lists

congusbongus picture congusbongus · Feb 11, 2013 · Viewed 41.9k times · Source

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

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

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

Answer

Nicolas picture Nicolas · Feb 11, 2013
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))