Python list comprehension to join list of lists

PythonList Comprehension

Python Problem Overview


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

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

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

Python Solutions


Solution 1 - Python

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))

Solution 2 - Python

from itertools import chain

combined = [['hello'], ['world', 'foo', 'bar']]
single = [i for i in chain.from_iterable(combined)]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncongusbongusView Question on Stackoverflow
Solution 1 - PythonNicolasView Answer on Stackoverflow
Solution 2 - PythonakiraView Answer on Stackoverflow