Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Merge Two Lists in Python
Next article icon

InPython, concatenating two lists element-wise means merging their elements in pairs. This is useful when we need to combine data from twolistsinto one just like joining first names and last names to create full names.zip() function is one of the most efficient ways to combine two lists element-wise. It pairs up elements from two lists and allows us to perform operations on these pairs.

Python
a=[1,2,3]b=['a','b','c']# Use zip() to pair the elementsresult=[str(x)+str(y)forx,yinzip(a,b)]print(result)

Output
['1a', '2b', '3c']
  • zip() pairs each element of list a with the corresponding element of list b. We then concatenate each pair into a single string.

Other methods that we can use to concatenate two lists element-wise in Python are:

Using List Comprehension

List comprehensionis another efficient and compact way to concatenate two lists element-wise. It is concise and easy to understand.

Python
a=[1,2,3]b=['a','b','c']# Use list comprehension to concatenate element-wiseresult=[str(a[i])+str(b[i])foriinrange(len(a))]print(result)

Output
['1a', '2b', '3c']
  • This method uses list comprehension to loop through both lists and concatenate the elements in one line. It's efficient and readable.

Using the map() Function

map() is another functional approach where we apply a function to each element of two lists. It can be a bit more advanced but works well in more complex scenarios.

Python
a=[1,2,3]b=['a','b','c']# Use map() to concatenate elementsresult=list(map(lambdax,y:str(x)+str(y),a,b))print(result)

Output
['1a', '2b', '3c']
  • Here, we define a lambda function to concatenate the elements of a and b. map() applies this function to each pair of elements.

Using a Loop

The loop is the most basic way to concatenate two lists element-wise. It is less efficient for larger lists compared to the previous methods.

Python
a=[1,2,3]b=['a','b','c']# Create an empty list to store the resultresult=[]# Loop through the lists and concatenate element-wiseforiinrange(len(a)):result.append(str(a[i])+str(b[i]))print(result)

Output
['1a', '2b', '3c']
  • We loop through the lists, concatenate the elements at each index, and append the result to a new list.

Using numpy Arrays

If you're working with numerical data and need high performance,numpy arraysprovide an efficient way to concatenate two lists element-wise. This method is particularly fast for large lists of numbers.

Python
importnumpyasnpa=[1,2,3]b=['a','b','c']# Convert the lists to numpy arraysa=np.array(a,dtype=str)b=np.array(b,dtype=str)# Use numpy's vectorized operation to concatenateresult=np.core.defchararray.add(a,b)print(result)

Output
['1a' '2b' '3c']

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp