-1

I have a list(or it can be a dictionary):

A = [
 ['soda',9,3],
 ['cake',56,6],
 ['beer',17,10],
 ['candies',95,8],
 ['sugar',21,20]
]

And i need to find a multiply of last 2 values in each sublist and sum up this:

9*3+56*6+17*10+95*8+21*20

How can i do this?

Juan Caicedo
  • 1,425
  • 18
  • 31
Max
  • 33
  • 7

1 Answers1

1

It's a very basic question and has a really simple answer. Until you are sure that the format is the same, the following code will help you:

result = 0
for sub_list in A:
    result += sub_list[-1] * sub_list[-2]

The result variable will store the result you want. sub_result is one of the sublists in A in each iteration.

sub_list[-1] is the last element of sub-list and `sub_list[-2] is the element before that.

Mr Alihoseiny
  • 1,202
  • 14
  • 24