Flatten a list of lists in Python
This post will discuss how to flatten a list of lists in Python.
For instance, [[1, 2, 3], [4, 5], [6, 7, 8]] should be converted into list [1, 2, 3, 4, 5, 6, 7, 8].
1. Using itertools.chain() function
You can simply call itertools.chain(*iterables) to flatten a list of lists, as shown below:
|
1 2 3 4 5 6 7 8 9 |
import itertools if __name__ == '__main__': lists = [[1, 2, 3], [4, 5], [6, 7, 8]] joinedlist = list(itertools.chain(*lists)) print(joinedlist) # prints [1, 2, 3, 4, 5, 6, 7, 8] |
This can also be done using itertools.chain.from_iterable(iterable), which doesn’t require unpacking the list.
|
1 2 3 4 5 6 7 8 |
import itertools if __name__ == '__main__': lists = [[1, 2, 3], [4, 5], [6, 7, 8]] joinedlist = list(itertools.chain.from_iterable(lists)) print(joinedlist) # prints [1, 2, 3, 4, 5, 6, 7, 8] |
2. Using sum() function
You can also use the built-in function sum(iterable[, start]) with start value as an empty list.
|
1 2 3 4 5 6 7 8 |
import itertools if __name__ == '__main__': lists = [[1, 2, 3], [4, 5], [6, 7, 8]] joinedlist = sum(lists, []) print(joinedlist) # prints [1, 2, 3, 4, 5, 6, 7, 8] |
3. Using += operator
Python supports list operations like concatenation with the help of the + operator. To join chained input, you can either use the + or += operator as following:
|
1 2 3 4 5 6 7 8 9 |
if __name__ == '__main__': lists = [[1, 2, 3], [4, 5], [6, 7, 8]] joinedlist = [] for list in lists: joinedlist += list print(joinedlist) # prints [1, 2, 3, 4, 5, 6, 7, 8] |
4. Using List Comprehension
Another common solution is to use list comprehensions. This can be easily achieved using the extend() function, as demonstrated below:
|
1 2 3 4 5 6 7 8 |
if __name__ == '__main__': lists = [[1, 2, 3], [4, 5], [6, 7, 8]] joinedlist = [] [joinedlist.extend(list) for list in lists] print(joinedlist) # prints [1, 2, 3, 4, 5, 6, 7, 8] |
Here’s how you can do using nested list comprehensions.
|
1 2 3 4 5 6 7 |
if __name__ == '__main__': lists = [[1, 2, 3], [4, 5], [6, 7, 8]] joinedlist = [x for list in lists for x in list] print(joinedlist) # prints [1, 2, 3, 4, 5, 6, 7, 8] |
5. Using reduce operation
Another plausible way to flatten a list is to reduce with the add() or concat() function, as shown below:
|
1 2 3 4 5 6 7 8 9 10 |
import operator from functools import reduce if __name__ == '__main__': lists = [[1, 2, 3], [4, 5], [6, 7, 8]] joinedlist = reduce(operator.add, lists) print(joinedlist) # prints [1, 2, 3, 4, 5, 6, 7, 8] |
Here’s another variation that uses the custom add function:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
from functools import reduce def add(x, y): return x + y if __name__ == '__main__': lists = [[1, 2, 3], [4, 5], [6, 7, 8]] joinedlist = reduce(add, lists) print(joinedlist) # prints [1, 2, 3, 4, 5, 6, 7, 8] |
Alternatively, you can use the lambda expression to write small functions. Lambda takes several parameters and an expression combining these parameters and creates an anonymous function that returns the value of the expression:
|
1 2 3 4 5 6 7 8 9 10 |
from functools import reduce if __name__ == '__main__': lists = [[1, 2, 3], [4, 5], [6, 7, 8]] adder = lambda x, y: x+y joinedlist = reduce(adder, lists) print(joinedlist) # prints [1, 2, 3, 4, 5, 6, 7, 8] |
That’s all about flattening a list of lists in Python.
Also See:
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)