Open In App

Python | Concatenate two lists element-wise

Last Updated : 20 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes we come across this type of problem in which we require to leave each element of one list with the other. This type of problems usually occurs in developments in which we have the combined information, like names and surnames in different lists. Let’s discuss certain ways in which this task can be performed. 

Method #1 : Using list comprehension + zip() List comprehension does the task of concatenating the similar index elements. The task of zip function is concatenating the resultant string into a single list and return list. 

Python3




# Python3 code to demonstrate
# interlist element concatenation
# using list comprehension + zip()
 
# initializing lists 
test_list1 = ["Geeksfor", "i", "be"]
test_list2 = ['Geeks', 's', 'st']
 
# printing original lists
print ("The original list 1 is : " + str(test_list1))
print ("The original list 2 is : " + str(test_list2))
 
# using list comprehension + zip()
# interlist element concatenation
res = [i + j for i, j in zip(test_list1, test_list2)]
 
# printing result
print ("The list after element concatenation is : " +  str(res))


Output:

The original list 1 is : ['Geeksfor', 'i', 'be']
The original list 2 is : ['Geeks', 's', 'st']
The list after element concatenation is : ['GeeksforGeeks', 'is', 'best']

Time Complexity: O(n*n), where n is the length of the list test_list 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list 

  Method #2 : Using map() + lambda + zip() The task of mapping each index element with each other is performed by map function in this method and the functionality of addition is performed by lambda function. This method works only in Python2. 

Python




# Python code to demonstrate
# interlist element concatenation
# using map() + lambda + zip()
 
# initializing lists 
test_list1 = ["Geeksfor", "i", "be"]
test_list2 = ['Geeks', 's', 'st']
 
# printing original lists
print ("The original list 1 is : " + str(test_list1))
print ("The original list 2 is : " + str(test_list2))
 
# using map() + lambda + zip()
# interlist element concatenation
res = list(map(lambda(i, j): i + j, zip(test_list1, test_list2)))
 
# printing result
print ("The list after element concatenation is : " +  str(res))


Output:

The original list 1 is : ['Geeksfor', 'i', 'be']
The original list 2 is : ['Geeks', 's', 'st']
The list after element concatenation is : ['GeeksforGeeks', 'is', 'best']

Method #3 : Using reduce():

This code uses the reduce() function from the functools module to concatenate the elements of two lists list1 and list2. The zip() function is used to pair the elements of the two lists together, and the lambda function passed to reduce() combines each pair of elements using string concatenation. The reduce() function returns a single list containing the concatenated elements.

Time complexity: O(n) (To concatenate all elements in worst case)

Auxiliary space: O(n) (To store the concatenated elements in a new list)

Python3




# Initialize the lists
list1 = ["Hello", "Hi", "Good morning"]
list2 = ["world", "there", "all"]
 
# Use reduce() to concatenate the elements
from functools import reduce
result = reduce(lambda res, l: res + [l[0] + " " + l[1]], zip(list1, list2), [])
 
print(result)  # ["Hello world", "Hi there", "Good morning all"]
#This code is contributed by Edula Vinay Kumar Reddy


Output

['Hello world', 'Hi there', 'Good morning all']

Method #4: Using for loop:

Python3




test_list1 = ["Geeksfor", "i", "be"]
test_list2 = ['Geeks', 's', 'st']
# printing original lists
print ("The original list 1 is : " + str(test_list1))
print ("The original list 2 is : " + str(test_list2))
res = []
for i in range(len(test_list1)):
    res.append(test_list1[i]+test_list2[i])
print("The list after element concatenation is : " + str(res))
#This code is contributed by Jyothi pinjala.


Output

The original list 1 is : ['Geeksfor', 'i', 'be']
The original list 2 is : ['Geeks', 's', 'st']
The list after element concatenation is : ['GeeksforGeeks', 'is', 'best']

Time complexity: O(n) 
Auxiliary Space: O(n) 

Method#5:Using itertools

Python3




import itertools
 
test_list1 = ["Geeksfor", "i", "be"]
test_list2 = ['Geeks', 's', 'st']
 
# Printing original lists
print("The original list 1 is:", test_list1)
print("The original list 2 is:", test_list2)
 
result = list(map(''.join, itertools.zip_longest(test_list1, test_list2)))
 
print("The list after element concatenation is:", result)
#This code is contributed by Vinay Pinjala.


Output

The original list 1 is: ['Geeksfor', 'i', 'be']
The original list 2 is: ['Geeks', 's', 'st']
The list after element concatenation is: ['GeeksforGeeks', 'is', 'best']

Time complexity: O(n) 
Auxiliary Space: O(n) 



Previous Article
Next Article

Similar Reads

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,
5 min read
Python | Concatenate dictionary value lists
Sometimes, while working with dictionaries, we might have a problem in which we have lists as it's value and wish to have it cumulatively in single list by concatenation. This problem can occur in web development domain. Let's discuss certain ways in which this task can be performed. Method #1 : Using sum() + values() This is the most recommended m
5 min read
Python program to concatenate every elements across lists
Given 2 lists, perform concatenations of all strings with each other across list. Input : test_list1 = ["gfg", "is", "best"], test_list2 = ["love", "CS"] Output : ['gfg love', 'gfg CS', 'is love', 'is CS', 'best love', 'best CS'] Explanation : All strings are coupled with one another. Input : test_list1 = ["gfg", "best"], test_list2 = ["love", "CS"
4 min read
Python Program to Sort the matrix row-wise and column-wise
Given a n x n matrix. The problem is to sort the matrix row-wise and column wise.Examples: Input : mat[][] = { {4, 1, 3}, {9, 6, 8}, {5, 2, 7} } Output : 1 3 4 2 5 7 6 8 9 Input : mat[][] = { {12, 7, 1, 8}, {20, 9, 11, 2}, {15, 4, 5, 13}, {3, 18, 10, 6} } Output : 1 5 8 12 2 6 10 15 3 7 11 18 4 9 13 20 Approach: Following are the steps: Sort each r
4 min read
How to Zip two lists of lists in Python?
The normal zip function allows us the functionality to aggregate the values in a container. But sometimes, we have a requirement in which we require to have multiple lists and containing lists as index elements and we need to merge/zip them together. This is quite uncommon problem, but solution to it can still be handy. Let's discuss certain ways i
7 min read
Count Negative Numbers in a Column-Wise and Row-Wise Sorted Matrix
Find the number of negative numbers in a column-wise / row-wise sorted matrix M[][]. Suppose M has n rows and m columns. Example: Input: M = [-3, -2, -1, 1] [-2, 2, 3, 4] [4, 5, 7, 8] Output : 4 We have 4 negative numbers in this matrix We strongly recommend you to minimize your browser and try this yourself first. Naive Solution: Here's a naive, n
15+ min read
Python - Concatenate Kth element in Tuple List
While working with tuples, we store different data as different tuple elements. Sometimes, there is a need to print a specific information from the tuple. For instance, a piece of code would want just names to be printed of all the student data in concatenated format. Lets discuss certain ways how one can achieve solutions to this problem. Method #
8 min read
Python program to concatenate two Integer values into one
Given two integers a and b. The task is to concatenate these two integers into one integer. Examples: Input : a = 806, b = 91 Output : 80691 Input : a = 5, b = 1091 Output : 51091 Method 1: One method of achieving this can be counting the number of digits of second number. Then multiply the first number with 10^digits and adding both the numbers. B
2 min read
Concatenate two strings using Operator Overloading in Python
Operator Overloading refers to using the same operator to perform different tasks by passing different types of data as arguments. To understand how '+' operator works in two different ways in python let us take the following example C/C++ Code # taking two numbers a = 2 b = 3 # using '+' operator add them c = a+b # printing the result print("
2 min read
Python | Program to count number of lists in a list of lists
Given a list of lists, write a Python program to count the number of lists contained within the list of lists. Examples: Input : [[1, 2, 3], [4, 5], [6, 7, 8, 9]] Output : 3 Input : [[1], ['Bob'], ['Delhi'], ['x', 'y']] Output : 4 Method #1 : Using len() C/C++ Code # Python3 program to Count number # of lists in a list of lists def countList(lst):
5 min read
three90RightbarBannerImg