Python Concatenate two lists elementwise (original) (raw)
Python – Concatenate two lists element-wise
Last Updated : 17 Dec, 2024
In Python, concatenating two lists element-wise means merging their elements in pairs. This is useful when we need to combine data from two lists into 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 elements
result = [str(x) + str(y) for x, y in zip(a, b)]
print(result)
`
- 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:
Table of Content
Using List Comprehension
List comprehension is 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-wise
result = [str(a[i]) + str(b[i]) for i in range(len(a))]
print(result)
`
- 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 elements
result = list(map(lambda x, y: str(x) + str(y), a, b))
print(result)
`
- 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 result
result = []
Loop through the lists and concatenate element-wise
for i in range(len(a)): result.append(str(a[i]) + str(b[i]))
print(result)
`
- 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 arrays provide an efficient way to concatenate two lists element-wise. This method is particularly fast for large lists of numbers.
Python `
import numpy as np
a = [1, 2, 3] b = ['a', 'b', 'c']
Convert the lists to numpy arrays
a = np.array(a, dtype=str) b = np.array(b, dtype=str)
Use numpy's vectorized operation to concatenate
result = np.core.defchararray.add(a, b)
print(result)
`
Similar Reads
- Python Lists In Python, a list is a built-in dynamic sized array (automatically grows and shrinks). We can store all types of items (including another list) in a list. A list may contain mixed type of items, this is possible because a list mainly stores references at contiguous locations and actual items maybe s 6 min read
- Get a list as input from user in Python We often encounter a situation when we need to take a number/string as input from the user. In this article, we will see how to take a list as input from the user using Python. Get list as input Using split() MethodThe input() function can be combined with split() to accept multiple elements in a si 3 min read
- Create List of Numbers with Given Range - Python The task of creating a list of numbers within a given range involves generating a sequence of integers that starts from a specified starting point and ends just before a given endpoint. For example, if the range is from 0 to 10, the resulting list would contain the numbers 0, 1, 2, 3, 4, 5, 6, 7, 8 3 min read
- Python - Add List Items Python lists are dynamic, which means we can add items to them anytime. In this guide, we'll look at some common ways to add single or multiple items to a list using built-in methods and operators with simple examples: Add a Single Item Using append()append() method adds one item to the end of the l 3 min read
- How to add Elements to a List in Python In Python, lists are dynamic which means that they allow further adding elements unlike many other languages. In this article, we are going to explore different methods to add elements in a list. For example, let's add an element in the list using append() method: [GFGTABS] Python a = [1, 2, 3] a.ap 2 min read