1

I'm working on Python and I want to make an element-wise sum of each list within 3 lists of lists. I'll try to simplify the problem to explain better.

Input:

a = [['alpha','beta','gamma'],['delta','epsilon','zeta'],['eta','theta','iota']]
b = [['AB'],['CD'],['EF']]
c = [['1','2','3'],['4','5','6'],['7','8','9']]

The outcome I need is:

d = [['alpha','beta','gamma','AB','1','2','3'],['delta','epsilon','zeta','CD','4','5','6'],['eta','theta','iota','EF','7','8','9']]

What I tried is:

d = []
for x in a:
    y = [a[x] + b[x] + c[x]]
    d.append(y)

However I get the error "TypeError: list indices must be integers or slices, not list" because x is defined as a list equal to ['alpha','beta','gamma']

1
  • d = [x + y + z for x, y, z in zip(a, b, c)] Commented Jan 12, 2023 at 23:07

3 Answers 3

1

Yes, you can 'add up' the sub lists to form new lists in d:

a = [[1,2,3], [4,5,6], [7,8,9]]
b = [[11,12,13], [14,15,16], [17,18,19]]
c = [[21,22,23], [24,25,26], [27,28,29]]

d = []
for a_i,b_i,c_i in zip(a,b,c):
    d.append(a_i + b_i + c_i)

print(d)

Output as requested.

In fact, you can use the built-in sum():

d = []
for items in zip(a, b, c):
    d.append(sum(items, start=[]))

print(d)
Sign up to request clarification or add additional context in comments.

9 Comments

Thank you, but in both cases I get this error: TypeError: can only concatenate list (not "str") to list
@quamrana You can push it even further: d = list(map(lambda items: sum(items, start=[]), zip(a, b, c))).
@TeresaBonserio: I don't see how that can be. I tried the code first, saw the correct output and pasted it here as an answer. Are your variables a, b, c different to what you posted?
Unfortunately I still get this error: TypeError: can only concatenate list (not "str") to list
Yes, the variables I posted are just an example of what I have in reality. The lists of lists contain text, not numbers. I don't know if that could be the reason why
|
0

Try numpy:

import numpy as np
    
a = [[1,2,3],[4,5,6],[7,8,9]]
b = [[11,12,13],[14,15,16],[17,18,19]]
c = [[21,22,23],[24,25,26],[27,28,29]]

a = np.array(a)
b = np.array(b)
c = np.array(c)
    
d = np.concatenate([a, b, c], axis=1)
    
print(d)
#[[ 1  2  3 11 12 13 21 22 23]
#[ 4  5  6 14 15 16 24 25 26]
#[ 7  8  9 17 18 19 27 28 29]]

3 Comments

Thank you, but I get this error: ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s)
@Teresa, try it: '''for x in range(len(a)): y = [a[x] + b[x] + c[x]] d.append(y)'''
If I try it for the original lists of lists I get this error: TypeError: can only concatenate list (not "str") to list. If I try it after converting them to arrays I get this error: UFuncTypeError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U14'), dtype('<U2')) -> None
0

You can you the zip function and the operator + at list it will join all the i groups

d = [x + y + z for x, y, z in zip(a, b, c)]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.