# Python | Multiply each element in a sublist by its index

Given a list of lists, the task is to multiply each element in a sublist by its index and return a summed list.

Given below are a few methods to solve the problem.

Method #1: Using Naive Method

 `# Python3 code to demonstrate ` `# to multiply numbers with position ` `# and add them to return num ` ` `  `import` `numpy as np ` ` `  `# initialising list ` `ini_list ``=` `[[``3``, ``4``, ``7``], [ ``6``, ``7``, ``8``], [ ``10``, ``7``, ``5``], [ ``11``, ``12``, ``13``]] ` ` `  `# printing initial_list ` `print` `(``"initial_list "``, ini_list) ` ` `  `res ``=` `[] ` `# Using Naive Method ` `for` `sub_list ``in` `ini_list: ` `    ``sublistsum ``=` `0` ` `  `    ``for` `i, value ``in` `enumerate``(sub_list): ` `        ``sublistsum ``=` `sublistsum ``+` `i ``*` `value ` ` `  `    ``res.append(sublistsum) ` ` `  `# printing result ` `print` `(``"result"``, res) `

Output:

```initial_list  [[3, 4, 7], [6, 7, 8], [10, 7, 5], [11, 12, 13]]
result [18, 23, 17, 38]
```

Method #2: Using List comprehension

 `# Python3 code to demonstrate ` `# to multiply numbers with position ` `# and add them to return num ` ` `  ` `  `# initialising list ` `ini_list ``=` `[[``3``, ``4``, ``7``], [ ``6``, ``7``, ``8``], [ ``10``, ``7``, ``5``], [ ``11``, ``12``, ``13``]] ` ` `  `# printing initial_list ` `print` `(``"initial_list "``, ini_list) ` ` `  `# Using list comprehension ` `res ``=` `[``sum``(i ``*` `j ``for` `i, j ``in` `enumerate``(sublist))  ` `                         ``for` `sublist ``in` `ini_list] ` ` `  `# printing result ` `print` `(``"result"``, res) ` `            `

Output:

```initial_list  [[3, 4, 7], [6, 7, 8], [10, 7, 5], [11, 12, 13]]
result [18, 23, 17, 38]
```

Method #3: Using numpy

 `# Python3 code to demonstrate ` `# to multiply numbers with position ` `# and add them to return num ` ` `  `import` `numpy as np ` ` `  `# initialising list ` `ini_list ``=` `[[``3``, ``4``, ``7``], [ ``6``, ``7``, ``8``], [ ``10``, ``7``, ``5``], [ ``11``, ``12``, ``13``]] ` ` `  `# printing initial_list ` `print` `(``"initial_list "``, ini_list) ` ` `  `# Using numpy ` `res ``=` `[np.arange(``len``(sublist)).dot(sublist) ``for` `sublist ``in` `ini_list] ` ` `  `# printing result ` `print` `(``"result"``, res) ` `             `  `            `

Output:

```initial_list  [[3, 4, 7], [6, 7, 8], [10, 7, 5], [11, 12, 13]]
result [18, 23, 17, 38]
```

My Personal Notes arrow_drop_up Programming freaktech Enthusiast and have interest in learning new upcoming technologies

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.