Python – Find Product of Index Value and find the Summation


When it is required to find the product of the index value and the summation, the ‘enumerate’ attribute is used.

Example

Below is a demonstration of the same

my_list = [71, 23, 53, 94, 85, 26, 0, 8]

print("The list is :")
print(my_list)

my_result = 0

for index, element in enumerate(my_list):
   my_result += (index + 1) * element

print("The resultant sum is :")
print(my_result)

Output

The list is :
[71, 23, 53, 94, 85, 26, 0, 8]
The resultant sum is :
1297

Explanation

  • A list of integers is defined and is displayed on the console.

  • An integer value is assigned to 0.

  • The enumerate value is used to iterate through the list.

  • The index is multipled with the respective element and this is added to the integer value.

  • This is the output that is displayed on the console.

Updated on: 20-Sep-2021

78 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements