Horje
How to Concatenate Two Lists Index Wise in Python

Index-wise concatenation of two lists refers to adding or combining elements of the given list based on their indices. There are many real-world applications of concatenating two lists index-wise, such as data processing, string manipulation, generating composite keys or super keys(in the database), and so on. In this article, we are going to deep dive into various methods through which we can achieve this task in Python.

For Example:

Input 1: ['gf', 'i', 'be']  
Input 2: ['g', 's', 'st']
Output : ['gfg', 'is', 'best']
Explaination: concatenate two list based on their index

Concatenating Lists Index Wise in Python

To concatenate two lists index-wise, you need to iterate through both lists simultaneously and combine their corresponding elements. This can be achieved using a loop or comprehensions, and the zip function is particularly handy for this purpose.

In Python, there are several ways to concatenate two list based on their index. Let us see how we can achieve this with code examples.

Using for-loop

In this method, we will use for loop to iterate over each element of both of the given list. After acquiring the elements from both lists, we will merge or concatenate them and add to a new resultant list.

Python
#input Lists
list1=["gf","i","be"]
list2=["g","s","st"]

#defining the resultanat list
ans = []

#iterating through each elements of both the lists
for i in range(0,min(len(list1),len(list2))):
    ans.append(list1[i]+ list2[i])
    
#Output    
print(ans)

Output:

['gfg', 'is', 'best']

Using List Comprehension

Python list comprehension is another way to iterate over lists and store the output in another list.

Python
#input Lists
list1=["gf","i","be"]
list2=["g","s","st"]

#List Comphrension 
ans = [list1[i] + list2[i] for i in range(min(len(list1),len(list2)))]
print(ans)

Output:

['gfg', 'is', 'best']

Using map()

The map() function apply or performs specified operation to the iterable, which is generally a list, set, tuple, etc. and return a map object. It also used the Lambda function to achieve this task.

Python
#input lists
list1 = ["gf", "i", "be"]
list2 = ["g", "s", "st"]

ans = list(map(lambda i, j: i + j, list1, list2))

#output
print(ans)

Output:

['gfg', 'is', 'best']

Using zip()

In this method, we are going to use Python’s zip() function. The zip() function basically combines more than 1 iterable objects ( such as dictionary, list, tuples etc) into a single iterator of tuples. Suppose, we combine two lists using zip(), then it will combine elements of similar index into one tuple and so on.

Python
#initalizing the list
list1 = ["gf", "i", "be"]
list2 = ["g", "s", "st"]

#performing the zip() function
ans = [i[0]+i[1] for i in zip(list1, list2)]

#displaying the output
print(ans)

Output:

['gfg', 'is', 'best']

Using numpy

The numpy is a prominent Python library generally used in mathematical computations. In this method, we will use numpy library and its function np.char.add() to concate each element of first list with each element of the second list index wise.

Python
import numpy as np

#Converting list into numpay array 
list1 = np.array(["gf", "i", "be"])
list2 = np.array(["g", "s", "st"])

#applying the char.add() function on the arrays
ans = np.char.add(list1, list2).tolist()

#dispalying the output
print(ans)

Output:

['gfg', 'is', 'best']

Conclusion

In this article, we are covered several ways through which we can concatenate two list based on their index. This means list1’s first index element is concatenate with list2’s first index and so on. We have covered several ways like using for-loop, using zip() , using numpy and using map() function. We have covered detailed explanation for each method with code, output and explanations. Concatenate two list based on their indices have may real world use cases such as creating candidate key or super key (in database) , data processing and many more. Performing it in an efficient way is a great way to solve many time limit based problems.




Reffered: https://www.geeksforgeeks.org


Python

Related
Understanding Django: Model() vs Model.objects.create() Understanding Django: Model() vs Model.objects.create()
How to fix Python Multiple Inheritance generates "TypeError: got multiple values for keyword argument". How to fix Python Multiple Inheritance generates "TypeError: got multiple values for keyword argument".
Splitting Cells and Counting Unique Values in Python List Splitting Cells and Counting Unique Values in Python List
OneToOneField() vs ForeignKey() in Django OneToOneField() vs ForeignKey() in Django
How to Fix "Could Not Import pypandoc - Required to Package PySpark" How to Fix "Could Not Import pypandoc - Required to Package PySpark"

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
19