How to join list of lists in python?



In Python, a list is an ordered sequence that can hold several object types such as integer, character, or float.

In this article, we will show you how to join the list of lists(nested lists) using python. Now we see 4 methods to accomplish this task −

  • Using nested for loop

  • Using List comprehension

  • Using sum() function

  • Using NumPy module

Assume we have taken a list of lists containing some elements. We will join those list of lists and return the result using different methods as specified above.

Method 1: Using nested for loop

Algorithm (Steps)

  • Create a variable to store the input list of lists(nested list).

  • Create a new empty list for storing the resultant list

  • Use the for loop, to traverse till the length of the input list of lists using the len() function(The number of items in an object is returned by the len() method)

  • Take another for loop to traverse in each element of the nested lists

  • Use the append() function(adds the element to the list at the end) to add this element to the result list.

  • Printing the resultant list after joining the input list of lists.

Example

The following program returns the list after joining the input list of lists using the nested for loop −

# input list of lists (nested list) input_nestedlist = [[1, 3],[2, 6, 7],[9, 5, 12, 7]] print(input_nestedlist) # creating a new empty list for storing result resultList = [] # Traversing in till the length of the input list of lists for m in range(len(input_nestedlist)): # using nested for loop, traversing the inner lists for n in range (len(input_nestedlist[m])): # Add each element to the result list resultList.append(input_nestedlist[m][n]) # printing the resultant list after joining the list of lists print("Resultant list after joining the list of lists = ", resultList)

Output

On executing, the above program will generate the following output −

[[1, 3], [2, 6, 7], [9, 5, 12, 7]]
Resultant list after joining the list of lists = [1, 3, 2, 6, 7, 9, 5, 12, 7]

Method 2: List comprehension

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input list of lists(nested list).

  • Use the list comprehension to create a new list by joining all the elements of the nested list

When you want to create a new list based on the values of an existing list, list comprehension provides a concise syntax.
  • Print the resultant list after joining the input list of lists.

Example

The following program returns the list after joining the input list of lists using list comprehension −

# input list of lists (nested list) input_list = [["tutorialspoint", "python"], [2, 6, 7], [9, 5, 12, 7]] print(input_list) # Getting each element from nested Lists and storing them in a new list using list comprehension resultList = [element for nestedlist in input_list for element in nestedlist] # printing the resultant list after joining the list of lists print("Resultant list after joining list of lists = ", resultList)

Output

On executing, the above program will generate the following output −

[['tutorialspoint', 'python'], [2, 6, 7], [9, 5, 12, 7]]
Resultant list after joining list of lists = ['tutorialspoint', 'python', 2, 6, 7, 9, 5, 12, 7]

Method 3: Using sum() function

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Use the sum() function to concatenate nested lists to a single list by passing an empty list as a second argument to it.

The sum() function returns a number that represents the sum of all items in an iterable.

Syntax

sum(iterable, start)

Parameters

iterable(optional)− any sequence like list, tuple etc

start(optional)− A value appended/added to the return value

  • Print the resultant list after joining the input list of lists.

Example

The following program returns the list after joining the input list of lists using the sum() function −

# input nested lists input_listoflists = [["tutorialspoint", "python"], [2, 6, 7],[9, 5]] print(input_listoflists) # pass second argument as empty list to concatenate nested lists resultList = sum(input_listoflists, []) # printing the resultant list after joining the list of lists print("Resultant list after joining the list of lists:\n", resultList)

Output

[['tutorialspoint', 'python'], [2, 6, 7], [9, 5]]
Resultant list after joining the list of lists:
['tutorialspoint', 'python', 2, 6, 7, 9, 5]

Method 4: Using NumPy module

The Numpy library includes functions for concatenating the substring and flattening them into a single 1-Dimensional list.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Use the import keyword, to import the NumPy module

  • Join the list of lists using the concatenate() function and flatten them into a 1-Dimensional list using the flat attribute and list() function(converts into a list) respectively

  • Print the resultant list after joining the input list of lists.

Example

The following program returns the list after joining the input list of lists using NumPy module −

# importing numpy module import numpy # input list of lists (nested list) input_listoflists = [["tutorialspoint", "python"], [2, 6, 7],[9, 5]] print(input_listoflists) # joining the list of lists using the concatenate() function and # flattening them into a 1-Dimensional list using the flat attribute # and list() function respectively resultList = list(numpy.concatenate(input_listoflists).flat) # printing the resultant list after joining the list of lists print("Resultant list after joining the list of lists:\n", resultList)

Output

[['tutorialspoint', 'python'], [2, 6, 7], [9, 5]]
Resultant list after joining the list of lists:
['tutorialspoint', 'python', '2', '6', '7', '9', '5']

Conclusion

We learned how to join/concatenate a list of lists into a 1-dimension list using four different methods, including for loops, list comprehension, NumPy functions, and the sum() function, from this article. We also learned what happens when we pass the nested list of lists with an empty list to sum().


Advertisements