1

I have to multiply the same index of two lists and then find the sum of that.

Please help me out! Thank you.

Ninja
  • 23
  • 4

4 Answers4

1

Try this,

>>> A=[2, 3, -6, 7, 10, 11]
>>> B=[1, 2, 3, 4, 5, 6]
>>> sum([x * y for x, y in zip(A, B)])
134

Let me explain what I did in my answer, I used zip() python Built-in Function and this is what documentation mention about it.

Make an iterator that aggregates elements from each of the iterables.

Returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. The iterator stops when the shortest input iterable is exhausted. With a single iterable argument, it returns an iterator of 1-tuples. With no arguments, it returns an empty iterator.

Little bit confusing, right? Check the below example:-


>>> A=[2, 3, -6, 7, 10, 11]
>>> B=[1, 2, 3, 4, 5, 6]
>>> zip(A,B)
<zip at 0x1fde73e6f88>  # it display something like this (zip object)
>>> list(zip(A,B))  # for visualization purpose, convert zip object to list
[(2, 1), (3, 2), (-6, 3), (7, 4), (10, 5), (11, 6)]

enter image description here

I think you can get clear idea what happen inside zip() function. Then multiply each and every value in zip object using python List Comprehensions to answer your question more pythonic. So zip object now created for us a new value series using A and B list. We assigned those values for x and y, multiply them and save in list.

>>> [x * y for x, y in zip(A, B)]
[2, 6, -18, 28, 50, 66]

After all the steps we used sum() to calculate the Sum a list of numbers in Python.

>>> sum([2, 6, -18, 28, 50, 66])
134

That's all, if you didn't get anything please add a comment to this answer.

Community
  • 1
  • 1
Kushan Gunasekera
  • 7,268
  • 6
  • 44
  • 58
  • 1
    well explained even though the author explicitly asked for solutions without using zip – ash17 Dec 15 '22 at 09:19
0
AB = [A[i] * B[i] for i in range(len(A))]

sum(AB)

Alternatively, try

AB = [value_ * B[i] for i, value_ in enumerate(A)]

sum(AB)
Stoner
  • 846
  • 1
  • 10
  • 30
0

with list comprehension:

A = [2, 3, -6, 7, 10, 11]
B = [1, 2, 3, 4, 5, 6]

print (sum([A[i]*B[i] for i in range(len(A))]))

output:

134

your code:

def lists(A, B):
    C = 0
    for i in range(len(A)):
        C += (A[i] * B[i])
    return C # <-----

A = [2, 3, -6, 7, 10, 11]
B = [1, 2, 3, 4, 5, 6]

print (lists(A,B))

NOTE: you need to put your return statement out of the for loop. If return statement is reached during the execution of a function, it will exit from function, what does it mean in your case if you have return in your for loop, in first iteration return function will be reached and exit (you got result 2 because in first iteration yuo have 2*1)

ncica
  • 7,015
  • 1
  • 15
  • 37
0

I believe you want this:

 def lists(A,B):
     C = 0
     for i in range(len(A)):
         C += (A[i] * B[i])
     return C

Now you can call your method lists with lists A and B like this:

 A=[2, 3, -6, 7, 10, 11]
 B=[1, 2, 3, 4, 5, 6]

lists(A,B)

Which will return 134. Your code was wrong because of your indentation. You had put your return statement inside the for loop, so your code would return C value in the first iteration, which was 0 + 2*1.

Lucas Wieloch
  • 818
  • 7
  • 19