 GeeksforGeeks App
Open App Browser
Continue

# Python – Find Product of Index Value and find the Summation

Given a List of elements, write a Python program to perform the product of index and value and compute the summation.

Examples:

Input : test_list = [5, 3, 4, 9, 1, 2]
Output : 76
Explanation : 5 + (3*2) 6 + 12 + 36 + 5 + 12 = 76

Input : test_list = [5, 3, 4]
Output : 23
Explanation : 5 + (3*2) 6 + 12 = 23

Method #1: Using loop + enumerate()

In this, we iterate for each element along with its index using enumerate() and compute the product. Sum counter is maintained to update the intermediate sum of the product computed.

## Python3

 `# Python3 code to demonstrate working of``# Index Value Product Sum``# Using loop + enumerate()` `# initializing list``test_list ``=` `[``5``, ``3``, ``4``, ``9``, ``1``, ``2``]` `# printing original list``print``(``"The original list is : "` `+` `str``(test_list))` `res ``=` `0``for` `idx, ele ``in` `enumerate``(test_list):` `    ``# updating summation of required product``    ``res ``+``=` `(idx ``+` `1``) ``*` `ele` `# printing result``print``(``"The computed sum : "` `+` `str``(res))`

Output

```The original list is : [5, 3, 4, 9, 1, 2]
The computed sum : 76```

Time complexity: O(n) as it involves a single loop through the elements of the list test_list with a size of n. The loop operation takes constant time. Hence, the overall time complexity is linear with respect to the size of the list.
Auxiliary space: O(1), as the variables used to store intermediate values (such as “res”) are not growing with the size of the input data and their usage is constant throughout the program.

Method #2 : Using sum() + list comprehension + enumerate()

One liner way to solve this problem, in this, we perform task of getting products iteration as list comprehension and summation at end is done using sum().

## Python3

 `# Python3 code to demonstrate working of``# Index Value Product Sum``# Using loop + enumerate()` `# initializing list``test_list ``=` `[``5``, ``3``, ``4``, ``9``, ``1``, ``2``]` `# printing original list``print``(``"The original list is : "` `+` `str``(test_list))` `# one liner to solve problem using list comprehension``res ``=` `sum``([(idx ``+` `1``) ``*` `ele ``for` `idx, ele ``in` `enumerate``(test_list)])` `# printing result``print``(``"The computed sum : "` `+` `str``(res))`

Output

```The original list is : [5, 3, 4, 9, 1, 2]
The computed sum : 76```

Time Complexity: O(n)
Auxiliary Space: O(1)

Method #3 : Using for loop

## Python3

 `# Python3 code to demonstrate working of``# Index Value Product Sum` `# initializing list``test_list ``=` `[``5``, ``3``, ``4``, ``9``, ``1``, ``2``]` `# printing original list``print``(``"The original list is : "` `+` `str``(test_list))` `res ``=` `0``for` `i ``in` `range``(``0``,``len``(test_list)):``    ``res``+``=``(i``+``1``)``*``test_list[i]``# printing result``print``(``"The computed sum : "` `+` `str``(res))`

Output

```The original list is : [5, 3, 4, 9, 1, 2]
The computed sum : 76```

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #4: Using zip() function with a for loop

Use the zip() function to combine the values from two iterables, i.e., the range(1, len(test_list)+1) iterable and the test_list iterable. This creates a new iterable of tuples, where each tuple contains the corresponding element from both the iterables.

Then, use a for loop to iterate over this iterable of tuples. For each tuple, extract the first element (which is the index + 1) and the second element (which is the value from the original list), and then compute the product of these two elements. Add up all the products to get the final result.

## Python3

 `# Python3 code to demonstrate working of``# Index Value Product Sum` `# initializing list``test_list ``=` `[``5``, ``3``, ``4``, ``9``, ``1``, ``2``]` `# printing original list``print``(``"The original list is : "` `+` `str``(test_list))` `# Method #4: Using zip() function with a for loop``res ``=` `0``for` `i, val ``in` `zip``(``range``(``1``, ``len``(test_list)``+``1``), test_list):``    ``res ``+``=` `i ``*` `val` `# printing result``print``(``"The computed sum : "` `+` `str``(res))`

Output

```The original list is : [5, 3, 4, 9, 1, 2]
The computed sum : 76```

Time complexity: O(n), where n is the length of the input list. The zip() function takes O(n) time to create the new iterable of tuples, and then the for loop iterates over this iterable once, taking O(n) time.
Auxiliary space: O(1)

Method 5: Using the reduce() function from the functools module

Step-by-step approach:

• Import the functools module.
• Define a function that multiplies each element of a list by its index plus one.
• Apply the function to each pair of index and element using reduce().
• Print the result.

Below is the implementation of the above approach:

## Python3

 `import` `functools` `# initializing list``test_list ``=` `[``5``, ``3``, ``4``, ``9``, ``1``, ``2``]` `# printing original list``print``(``"The original list is : "` `+` `str``(test_list))` `# define function to multiply each element by its index plus one``def` `multiply_by_index_plus_one(indexed_element):``    ``index, element ``=` `indexed_element``    ``return` `(index ``+` `1``) ``*` `element` `# apply function using reduce() and sum the results``res ``=` `functools.``reduce``(``lambda` `x, y: x ``+` `y, ``map``(multiply_by_index_plus_one, ``enumerate``(test_list)))` `# printing result``print``(``"The computed sum : "` `+` `str``(res))`

Output

```The original list is : [5, 3, 4, 9, 1, 2]
The computed sum : 76```

Time complexity: O(n) because it iterates over the list only once.
Auxiliary space: O(1) because it doesn’t create any additional data structures.

My Personal Notes arrow_drop_up