Sum of two lists with different length

Hi,
I need to sum values in two different list of different lengths.
List 1 (0,0,0)
+
List 2 (0,1)
I would like this result (0,1,0).
I try with managing lacing but any result!
I post an image to explain!!


Perhaps I am missing something obvious.
Thx!

@SalvoC

thank you @salvatoredragotta .
and just with standard node no solution?
thx

1 Like

try this

1 Like

Perhaps a solution around List.Transpose and Math.Sum will suit here?

Example of @jacob.small’s solution:

Bonus points for an example of a while loop for the hell of it @Draxl_Andreas:

def matadd(a, b):
  ret = []
  c = 0
  while True:
    if c > len(a)-1 and c > len(b)-1:
      break
    elif c > len(a)-1:
      z = b[c]
    elif c > len(b)-1:
      z = a[c]
    else:
      z = a[c] + b[c]
    ret.append(z)
    c += 1
  return ret

OUT = map(matadd, IN[0], IN[1])

but a better python version in my opinion:

def tryadd(alist, ind):
	try: return alist[ind]
	except IndexError: return 0

def matadd(a, b):
    ret = []
    for i in range(max([len(a),len(b)])):
    	s = tryadd(a,i)
    	s += tryadd(b,i)
    	ret.append(s)
    return ret

OUT = map(matadd, IN[0], IN[1])
4 Likes