How To Join A List Of Lists In Python

Join a list of lists in Python

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:

Leave a Reply

Your email address will not be published. Required fields are marked *