Friday, November 15, 2024
Google search engine
HomeLanguagesPython – Concatenate two list of lists Row-wise

Python – Concatenate two list of lists Row-wise

Given two matrices, the task is to write a Python program to add elements to each row from initial matrix.

Input : test_list1 = [[4, 3, 5,], [1, 2, 3], [3, 7, 4]], test_list2 = [[1, 3], [9, 3, 5, 7], [8]]
Output : [[4, 3, 5, 1, 3], [1, 2, 3, 9, 3, 5, 7], [3, 7, 4, 8]]
Explanation : Matrix is row wise merged.

Input : test_list1 = [[4, 3, 5,], [1, 2, 3], [3, 7, 4]], test_list2 = [[1], [9], [8]]
Output : [[4, 3, 5, 1], [1, 2, 3, 9], [3, 7, 4, 8]]
Explanation : Matrix is row wise merged.

Method #1: Using enumerate() + loop

In this, we get each index of each initial matrix and append its all elements to the second matrix’s corresponding row.

Python3




# Python3 code to demonstrate working of
# Concatenate 2 Matrix Row-wise
# Using loop + enumerate()
 
# initializing lists
test_list1 = [[4, 3, 5, ], [1, 2, 3], [3, 7, 4]]
test_list2 = [[1, 3], [9, 3, 5, 7], [8]]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
for idx, ele in enumerate(test_list1):
    new_vals = []
 
    # getting all values at same index row
    for ele in test_list2[idx]:
        new_vals.append(ele)
 
    # extending the initial matrix
    test_list1[idx].extend(new_vals)
 
# printing result
print("The concatenated Matrix : " + str(test_list1))


Output:

The original list 1 is : [[4, 3, 5], [1, 2, 3], [3, 7, 4]]

The original list 2 is : [[1, 3], [9, 3, 5, 7], [8]]

The concatenated Matrix : [[4, 3, 5, 1, 3], [1, 2, 3, 9, 3, 5, 7], [3, 7, 4, 8]]

Time Complexity: O(n*n)
Auxiliary Space: O(n)

Method #2: Using zip() + list comprehension

In this, we perform the task of concatenating rows using zip(), and iteration through each row happens using list comprehension.

Python3




# Python3 code to demonstrate working of
# Concatenate 2 Matrix Row-wise
# Using zip() + list comprehension
 
# initializing lists
test_list1 = [[4, 3, 5, ], [1, 2, 3], [3, 7, 4]]
test_list2 = [[1, 3], [9, 3, 5, 7], [8]]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# zip() combines the results
# list comprehension provides shorthand
res = list(sub1 + sub2 for sub1, sub2 in zip(test_list1, test_list2))
 
# printing result
print("The concatenated Matrix : " + str(res))


Output:

The original list 1 is : [[4, 3, 5], [1, 2, 3], [3, 7, 4]]

The original list 2 is : [[1, 3], [9, 3, 5, 7], [8]]

The concatenated Matrix : [[4, 3, 5, 1, 3], [1, 2, 3, 9, 3, 5, 7], [3, 7, 4, 8]]

The time and space complexity of both the methods is same:

Time Complexity: O(n2)

Space Complexity: O(n)

Method #3: Using extend()+loop

Python3




# Python3 code to demonstrate working of
# Concatenate 2 Matrix Row-wise
 
# initializing lists
test_list1 = [[4, 3, 5, ], [1, 2, 3], [3, 7, 4]]
test_list2 = [[1, 3], [9, 3, 5, 7], [8]]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
for i in range(0, len(test_list1)):
    test_list1[i].extend(test_list2[i])
# printing result
print("The concatenated Matrix : " + str(test_list1))


Output

The original list 1 is : [[4, 3, 5], [1, 2, 3], [3, 7, 4]]
The original list 2 is : [[1, 3], [9, 3, 5, 7], [8]]
The concatenated Matrix : [[4, 3, 5, 1, 3], [1, 2, 3, 9, 3, 5, 7], [3, 7, 4, 8]]

Time complexity: O(n*n), where n is the length of the test_list. The extend()+loop takes O(n*n) time
Auxiliary Space: O(n), extra space of size n is required

Method #4: Using numpy

Note: Install numpy module using command “pip install numpy”

Python3




#Importing numpy library
import numpy as np
 
#initializing lists
test_list1 = [[4, 3, 5, ], [1, 2, 3], [3, 7, 4]]
test_list2 = [[1, 3], [9, 3, 5, 7], [8]]
 
#Printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
#Using numpy to concatenate two lists row-wise
res = np.concatenate((test_list1, test_list2), axis = 1)
 
#printing result
print("The concatenated Matrix : " + str(res))


Output:

The original list 1 is : [[4, 3, 5], [1, 2, 3], [3, 7, 4]]

The original list 2 is : [[1, 3], [9, 3, 5, 7], [8]]

The concatenated Matrix : [[4, 3, 5, 1, 3], [1, 2, 3, 9, 3, 5, 7], [3, 7, 4, 8]]

Time Complexity: O(n^2)
Auxiliary Space: O(n)

Method #5: Using itertools.chain() and map()

This method uses the itertools.chain() function to chain the two input matrices, and the map() function to convert the chained iterable into a list of lists representing the concatenated matrix.

  • Import the itertools module.
  • Initialize the two matrices test_list1 and test_list2.
  • Use the zip() function to pair up corresponding rows of the two matrices.
  • For each pair of rows, use itertools.chain() to concatenate them row-wise.
  • Convert each concatenated row back into a list.
  • Collect all the concatenated rows into a new list, which represents the final concatenated matrix.

Python3




import itertools
 
# initializing lists
test_list1 = [[4, 3, 5, ], [1, 2, 3], [3, 7, 4]]
test_list2 = [[1, 3], [9, 3, 5, 7], [8]]
 
# using itertools.chain() and zip() to concatenate the two matrices row-wise
concatenated_matrix = [list(itertools.chain(*x)) for x in zip(test_list1, test_list2)]
 
# printing result
print("The concatenated Matrix : " + str(concatenated_matrix))


Output

The concatenated Matrix : [[4, 3, 5, 1, 3], [1, 2, 3, 9, 3, 5, 7], [3, 7, 4, 8]]

Time complexity of the code is O(NM), where N is the number of lists in test_list1 and test_list2, and M is the maximum length of the lists.

The auxiliary space of the code is O(NM), since a new list of length N*M is created to store the concatenated matrix. 

RELATED ARTICLES

Most Popular

Recent Comments