Convert List of Multiple Lists to Single List in Python (3 Examples)
Hi! This tutorial will show you 3 simple examples of how to turn a list of multiple lists into a single list in the Python programming language.
First, though, here is a quick overview of this tutorial:
Let’s dive into Python code!
Create Sample List of Lists
We will create a sample list of lists that we will turn into a single list in this tutorial. Therefore, in your Python IDE, run the line of code below to create a nested list containing multiple lists.
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] print(type(nested_list)) # <class 'list'>
Example 1: Transform List of Multiple Lists into Single List Using Nested For Loop & append()
In this first example, we will use a nested for loop and append() function to transform the list of multiple lists into a single list.
flat_list =  for sublist in nested_list: for item in sublist: flat_list.append(item) print(flat_list) # [1, 2, 3, 4, 5, 6, 7, 8, 9] print(type(flat_list)) # <class 'list'>
In the example above, the outer for loop looped through the nested list and returned the sublists, while the inner for loop looped through each item in the sublists and appended them to the flat_list object.
Example 2: Transform List of Multiple Lists into Single List Using List Comprehension
In this second example, we will transform our nested list into a single list, using list comprehension method:
flat_list = [item for sublist in nested_list for item in sublist] print(flat_list) # [1, 2, 3, 4, 5, 6, 7, 8, 9] print(type(flat_list)) # <class 'list'>
The list comprehension works exactly like the nested for loop in the previous example. The items in the nested list are unpacked inside the flat_list object to create a single list. It is a more concise approach to solving the problem.
Example 3: Transform List of Multiple Lists into Single List Using itertools
In this third and final example, we will import Python’s itertools, and use it to turn the list of multiple lists into a single list, like so.
import itertools flat_list = list(itertools.chain(*nested_list)) print(flat_list) # [1, 2, 3, 4, 5, 6, 7, 8, 9] print(type(flat_list)) # <class 'list'>
itertools.chain() function combines the multiple sublists into a single iterable object, and then the
list() function converts the iterable object into a single list.
With that, we have demonstrated 3 simple ways to convert a list of multiple lists into a single list in Python. I hope you found this tutorial helpful!
Video, Further Resources & Summary
Do you need more explanations on how to convert a list of multiple lists into a single list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
In the video, we explain in some more detail how to convert a list of multiple lists into a single list in Python.
The YouTube video will be added soon.
Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:
- Convert Multiple Lists into a Dictionary in Python (3 Examples)
- Convert Lists to Bytes & Vice-Versa in Python (Examples)
- Access Dictionary within List in Python (Example)
- Learn Python Programming
- Sort List of datetime Objects in Python (Example)
This post has shown how to convert a list of multiple lists into a single list in Python. In case you have further questions, you may leave a comment below.
This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.
Statistics Globe Newsletter