EDIT: Thank you for the responses, which are helpful, but I'm not sure they're getting at the core of my problem. In some cases it would be nice to simply select the lower of the lists and then add the values accordingly, but in THIS case I actually want to treat uneven lists as though the shorter list had zero values for the values it was missing. So I want [1, 2, 3] + [1, 2] to function as [1, 2, 3] + [1, 2, 0]. I don't think zip, or reversing my operator, will accomplish this.
I'm trying to create a function that adds the corresponding values of two lists and returns a new list with the sums of each of the indices of the two original lists:
def addVectors(v1, v2):
print(v1[0], v1[1], v2[0])
newVector = []
if len(v1) > len(v2):
for index in range(len(v1)):
print(index)
newVector[index] += v1[index] + v2[index]
else:
for index in range(len(v2)):
print(index)
newVector[index] += v2[index] + v1[index]
return newVector
addVectors([1, 2, 3], [1, 2])
Yet I'm getting an error stating that the list index is out of range? Not sure what I'm doing wrong in this seemingly simple program....
3value? Your lists are of unequal length, so3has no corresponding value.zipis given two lists of unequal length, it goes to the shorter one.