how to concatenate multiple lists element wise

  • Last Update :
  • Techknowledgy :

The zip function can take in the two lists as parameters and concatenate them. We design a for loop to capture these combinations and put them into a new list.,The map function will apply the same function again and again to the parameters passed onto it. We will also use a lambda function to combine the individual elements one by one from the two lists through zip.,Pyhton has great data manipulation features. In this article we will see how to combine the elements from two lists in the same order as they are present in the lists.,Compute the bit-wise OR of two boolean arrays element-wise in Numpy

listA = ["Outer-", "Frost-", "Sun-"]
listB = ['Space', 'bite', 'rise']
# Given lists
print("Given list A: ", listA)
print("Given list B: ", listB)
# Use zip
res = [i + j
   for i, j in zip(listA, listB)
]
# Result
print("The concatenated lists: ", res)

Running the above code gives us the following result −

Given list A: ['Outer-', 'Frost-', 'Sun-']
Given list B: ['Space', 'bite', 'rise']
The concatenated lists: ['Outer-Space', 'Frost-bite', 'Sun-rise']
listA = ["Outer-", "Frost-", "Sun-"]
listB = ['Space', 'bite', 'rise']
# Given lists
print("Given list A: ", listA)
print("Given list B: ", listB)
# Use map
res = list(map(lambda(i, j): i + j, zip(listA, listB)))
# Result
print("The concatenated lists: ", res)

Suggestion : 2

Use zip:

>>> ["{}{:02}".format(b_, a_) for a_, b_ in zip(a, b)]
['asp100', 'asp101', 'asp105', 'asp106', 'asp210', 'asp211']

Using zip

[m + str(n) for m, n in zip(b, a)]

output

['asp10', 'asp11', 'asp15', 'asp16', 'asp210', 'asp211']

Other solution (preferring printf formating style over .format() usage), it's also smaller:

>>> ["%s%02d" % t
   for t in zip(b, a)
]
['asp100', 'asp101', 'asp105', 'asp106', 'asp210', 'asp211']

Than can be done elegantly with map and zip:

map(lambda(x, y): x + y, zip(list1, list2))

Example:

In[1]: map(lambda(x, y): x + y, zip([1, 2, 3, 4], [4, 5, 6, 7]))
Out[1]: [5, 7, 9, 11]

inputs:

a = [0, 1, 5, 6, 10, 11]
b = ['asp1', 'asp1', 'asp1', 'asp1', 'asp2', 'asp2']

concat_func = lambda x, y: x + "" + str(y)

list(map(concat_func, b, a)) # list the map
function

output:

['asp10', 'asp11', 'asp15', 'asp16', 'asp210', 'asp211']

If you wanted to concatenate arbitrary number of lists, you could do this:

In[1]: lists = [
   ["a", "b", "c"],
   ["m", "n", "o"],
   ["p", "q", "r"]
] # Or more

In[2]: lists
Out[2]: [
   ['a', 'b', 'c'],
   ['m', 'n', 'o'],
   ['p', 'q', 'r']
]

In[4]: list(map("".join, zip( * lists)))
Out[4]: ['amp', 'bnq', 'cor']

Suggestion : 3

Last Updated : 28 Jan, 2019

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']

Suggestion : 4

Add two lists element-wise using zip(),Add two lists element-wise using map(),Add two lists element-wise using NumPy,Add two lists element wise using numpy.add()

Let’s see an example,

first = [11, 12, 13, 14, 15, 16]
second = [71, 77, 89, 51, 90, 59]

# Add two lists element - wise using zip() & sum()
final_list = [sum(value) for value in zip(first, second)]

print(final_list)
2._
[82, 89, 102, 65, 105, 75]

We passed the two list objects first and second to the zip() function. It aggregated both the lists and returned an iterable of tuples. The contents of this iterable tuple are like,

(11, 71)
(12, 77)
(13, 89)
(14, 51)
(15, 90)
(16, 59)

The map() function will iterate over both lists together. Like, in the first iteration it will pick the values 11 and 71. Then it will pass those values to the lambda function (first argument), which will return the sum of passed values i.e. 88. The map() function will append this value in a map object and will go forward with the second iteration. In the end, we will convert this map object to a list. This list will contain the sum of our two original lists element-wise. For example,

first = [11, 12, 13, 14, 15, 16]
second = [71, 77, 89, 51, 90, 59]

# Add two lists element - wise using zip() & Lambda
function
final_list = list(map(lambda a, b: a + b, first, second))

print(final_list)

Output:

[82, 89, 102, 65, 105, 75]

Suggestion : 5

This example uses for loop and append() function to add two lists element-wise. It allows lists of unequal lengths. It finds a smaller list between the two and then iterates over the elements of the shorter list using for loop. append() function returns the sum of two elements. The sum is appended to the resultant list. This method does not print the remaining elements of the longer list. It is a simple approach as it does not have the overhead of calling or importing any additional library.,This method allows lists of unequal lengths and does not print the remaining elements of the longer list. Here, we use two built-in functions - zip() and sum(). The sum() function adds list elements one by one using the index and the zip() function groups the two list elements together. It is the most pythonic way and it also increases the readability.,This is an alternative method of the NumPy library. Instead of using an operator, we use numpy.add() function. It takes Python Lists as input and prints the result. The drawback of this method is that it takes lists of equal lengths.,This method allows lists of unequal lengths and does not print the remaining elements. Here, we use two built-in functions - map() and add(). map() takes both input lists and add() function as arguments. add is imported from the operator module of Python. add() function simply adds up the elements of two lists and returns an iterable as output. We convert the iterable into a list using the list constructor method.

list1 = ["Ram", "Arun", "Kiran"]
list2 = [16, 78, 32, 67]
list3 = ["apple", "mango", 16, "cherry", 3.4]
#two input lists
list1 = [11, 21, 34, 12, 31, 26]
list2 = [23, 25, 54, 24, 20]

#empty resultant list
result = []

#choose the smaller list to iterate
small_list = len(list1) < len(list2) and list1 or list2

for i in range(0, len(small_list)):
   result.append(list1[i] + list2[i])

print("Resultant list : " + str(result))
#two input lists
list1 = [11, 21, 34, 12, 31]
list2 = [23, 25, 54, 24, 20, 27]

#empty resultant list
result = []

#choose the smaller list to iterate
small_list = len(list1) < len(list2) and list1 or list2

result = [list1[i] + list2[i]
   for i in range(len(small_list))
]

print(" Resultant list : " + str(result))
from operator
import add

#two input lists
list1 = [11, 21, 34, 12, 31]
list2 = [23, 25, 54, 24, 20, 27]

result = list(map(add, list1, list2))

print("Resultant list : " + str(result))
#two input lists
list1 = [11, 21, 34, 12, 31, 77]
list2 = [23, 25, 54, 24, 20]

result = [sum(i) for i in zip(list1, list2)]

print("Resultant list : " + str(result))
from itertools
import zip_longest

#two input lists
list1 = [11, 21, 34, 12, 31, 77]
list2 = [23, 25, 54, 24, 20]

result = [sum(x) for x in zip_longest(list1, list2, fillvalue = 0)]

print("Resultant list : " + str(result))