def run(first, second):
sum=[]
for i in range(len(first)):
third.append(second[i]+first[i])
return sum
print run([1,2,3],[10,20,30])
The code works fine and prints out a list with three elements where each element is the sum of the two elements of the same index in “second” and “first”, specifically [11, 22, 33]. Is there a more straightforward approach to return the same result?
Sure, use the
zip()built-in function and a list comprehension:Demo: