Python | Merge elements of sublists
Given two lists containing sublists, the task is to merge elements of sublist of two lists in a single list.
Examples:
Input:
list1 = [[1, 20, 30],
[40, 29, 72],
[119, 123, 115]]
list2 = [[29, 57, 64, 22],
[33, 66, 88, 15],
[121, 100, 15, 117]]
Output: [[1, 20, 30, 29, 57, 64, 22],
[40, 29, 72, 33, 66, 88, 15],
[119, 123, 115, 121, 100, 15, 117]]
Method #1: Using Map + lambda
# Python code to merge elements of sublists # Initialisation of first list list1 = [[1, 20, 30], [40, 29, 72], [119, 123, 115]] # Initialisation of second list list2 = [[29, 57, 64, 22], [33, 66, 88, 15], [121, 100, 15, 117]] # Using map + lambda to merge lists Output = list(map(lambda x, y:x + y, list1, list2)) # Printing output print(Output) |
chevron_right
filter_none
Output:
[[1, 20, 30, 29, 57, 64, 22], [40, 29, 72, 33, 66, 88, 15], [119, 123, 115, 121, 100, 15, 117]]
Method #2: Using Zip()
# Python code to merge elements of sublists # Initialisation of first list list1 = [[1, 20, 30], [40, 29, 72], [119, 123, 115]] # Initialisation of second list list2 = [[29, 57, 64, 22], [33, 66, 88, 15], [121, 100, 15, 117]] # Using zip to merge lists Output = [x + y for x, y in zip(list1, list2)] # Printing output print(Output) |
chevron_right
filter_none
Output:
[[1, 20, 30, 29, 57, 64, 22], [40, 29, 72, 33, 66, 88, 15], [119, 123, 115, 121, 100, 15, 117]]
Method #3: Using starmap() and concat()
# Python code to merge elements of sublists from operator import concat from itertools import starmap # Initialisation of first list list1 = [[1, 20, 30], [40, 29, 72], [119, 123, 115]] # Initialisation of second list list2 = [[29, 57, 64, 22], [33, 66, 88, 15], [121, 100, 15, 117]] # Using starmap() and concat to merge list Output = list(starmap(concat, zip(list1, list2))) # Printing output print(Output) |
chevron_right
filter_none
Output:
[[1, 20, 30, 29, 57, 64, 22], [40, 29, 72, 33, 66, 88, 15], [119, 123, 115, 121, 100, 15, 117]]
Recommended Posts:
- Python | Merge corresponding sublists from two different lists
- Python | Print the common elements in all sublists
- Python | Merge list elements
- Python | Merge first and last elements separately in a list
- Python | Adding value to sublists
- Python | Add the occurrence of each number as sublists
- Python | Print all sublists of a list
- Python | Remove all sublists outside the given range
- Python | Swapping sublists over given range
- Python | Remove repeated sublists from given list
- Python | Sort all sublists in given list of strings
- Python | Remove sublists that are present in another sublist
- Python | Split a list into sublists of given lengths
- Python | Count the sublists containing given element in a list
- Python | Count unique sublists within list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


