 # 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
```>>> ["{}{: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: map(lambda(x, y): x + y, zip([1, 2, 3, 4], [4, 5, 6, 7]))
Out: [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: lists = [
["a", "b", "c"],
["m", "n", "o"],
["p", "q", "r"]
] # Or more

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

In: list(map("".join, zip( * lists)))
Out: ['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

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

```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

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

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))```