To join a list of lists in Python, there are some solutions we have effectively tested. Follow the article to better understand.
What is a list in Python?
In Python, ‘list’ is a data type that allows storing many other data types. You can access the data in ‘list’ through the position of that element in ‘list’. ‘List’ is like an array in other programming languages.
Example:
myList = [[1, 2], [3, 4, 5], [6, 7, 8, 9]]
How to join a list of lists in Python?
Use nested for loop
In the first for loop, I use the len() function to traverse the input list (from beginning to end ). 2nd for loop to look through the elements in the nested list and then use the append() function to add the elements to the end of the list.
Example:
myList = [[1, 2], [3, 4, 5], [6, 7, 8, 9]] # Create an empty list to store the elements after merging emptyList = [] for i in range(len(myList)): for j in range(len(myList[i])): emptyList.append(myList[i][j]) print("List after merge = ", emptyList)
Output:
List after merge = [1, 2, 3, 4, 5, 6, 7, 8, 9]
In the example above, using a for loop to nest the merged elements into a list.
Use the NumPy module
Numpy is a robust math library that allows efficient work with matrices and arrays because of its high processing speed. I would use the numpy.concatenate() function to turn the list into a one-dimensional array, then use the list() function to convert the list back into a list.
Example:
import numpy myList = [["my name is ", "John"], ['I am'], [18]] # Use the list() function to convert the list back into a list mergeList = list(numpy.concatenate(myList).flat) print("List after merge = ", mergeList)
Output:
List after merge = ['my name is ', 'John', 'I am', '18']
In the example above, the list is flattened to a one-dimensional array by the numpy.concatenate() function, then aggregated to return a list using the list() function.
Use the sum() function
Use the sum() function to concatenate lists by passing an empty list as the function’s second argument.
Syntax:
sum(iterable, start )
Parameters:
- iterable: built-in iterables like string, list, dict,..
- start: value is added from the return value. The default is 0.
Example:
myList = [["My name is john. I am"], [18]] # The second input argument to the sum() function is an empty list mergeList = sum(myList, []) print("List after merge = ", mergeList)
Output:
List after merge =['My name is john. I am ', 18]
Use the itertools module
In this way, I will use the itertools.chain() function in the itertools module to be able to concatenate strings.
Syntax:
itertools.chain(*iterable)
Parameters:
- iterable: built-in iterables like string, list, dict,..
Example:
import itertools myList = [["my name is ", "John"], ['I am '], [18]] mergelist = list(itertools.chain(*myList)) print("My List:", mergelist)
Output:
My List: ['my name is ', 'John', 'I am ', 18]
In example above, the chain() function takes multiple iterables and returns one iterables.
Summary
If you have any questions about how to join a list of lists in Python, leave a comment below. I will answer your questions.
Maybe you are interested:
- Multiply each element in a list by a number in Python
- split a string into a list of characters in python
- Add two lists element-wise in Python

My name is Jason Wilson, you can call me Jason. My major is information technology, and I am proficient in C++, Python, and Java. I hope my writings are useful to you while you study programming languages.
Name of the university: HHAU
Major: IT
Programming Languages: C++, Python, Java