Open In App

Concatenate two list of lists Row-wise-Python

Last Updated : 11 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The task of concatenate two lists of lists row-wise, meaning we merge corresponding sublists into a single sublist. For example, given a = [[4, 3], [1, 2]] and b = [[7, 5], [9, 6]], we pair elements at the same index: [4, 3] from a is combined with [7, 5] from b, resulting in [4, 3, 7, 5], and [1, 2] from a merges with [9, 6] from b, forming [1, 2, 9, 6]. The final output is [[4, 3, 7, 5], [1, 2, 9, 6]].

Using list comprehension with zip

List comprehension combined with zip() iterates through both lists simultaneously and concatenates corresponding sublists using the + operator. This method is concise and easy to read.

Python
a = [[4, 3, 5], [1, 2, 3], [3, 7, 4]]
b = [[1, 3], [9, 3, 5, 7], [8]]

a = [x + y for x, y in zip(a, b)]
print(a)

Output
[[4, 3, 5, 1, 3], [1, 2, 3, 9, 3, 5, 7], [3, 7, 4, 8]]

Explanation: List comprehension with zip() pairs corresponding sublists from a and b, forming tuples. The + operator concatenates each pair (x, y), appending elements of y to x, resulting in a new list of merged sublists.

Using map with zip

map() function applies a lambda function to each pair of sublists returned by zip(), concatenating them using the + operator. This method is functional and avoids explicit loops.

Python
a = [[4, 3, 5], [1, 2, 3], [3, 7, 4]]
b = [[1, 3], [9, 3, 5, 7], [8]]

a = list(map(lambda x: x[0] + x[1], zip(a, b)))
print(a)

Output
[[4, 3, 5, 1, 3], [1, 2, 3, 9, 3, 5, 7], [3, 7, 4, 8]]

Explanation: map() with zip() pairs corresponding sublists from a and b, merging each pair using + in a lambda function. The result is converted to a list of concatenated sublists.

Using a loop with extend

This method modifies the first list a in place by iterating over its elements and appending elements from the corresponding sublists in b using extend(). It is efficient when working with large datasets.

Python
a = [[4, 3, 5], [1, 2, 3], [3, 7, 4]]
b = [[1, 3], [9, 3, 5, 7], [8]]

for i in range(len(a)):
    a[i].extend(b[i])

print(a)

Output
[[4, 3, 5, 1, 3], [1, 2, 3, 9, 3, 5, 7], [3, 7, 4, 8]]

Explanation: This code iterates through a, using extend() to append corresponding sublists from b, modifying a in place with merged sublists.

Using itertools.chain

chain() function from itertools flattens multiple iterables into a single iterable. Here, it is used inside a list comprehension to concatenate corresponding sublists.

Python
from itertools import chain

a = [[4, 3, 5], [1, 2, 3], [3, 7, 4]]
b = [[1, 3], [9, 3, 5, 7], [8]]

a = [list(chain(x, y)) for x, y in zip(a, b)]
print(a)

Output
[[4, 3, 5, 1, 3], [1, 2, 3, 9, 3, 5, 7], [3, 7, 4, 8]]

Explanation: This code pairs sublists using zip(), merges them with chain() and converts the result into a list, forming concatenated sublists.


Next Article
Practice Tags :

Similar Reads

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy