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

In Python, there are different approaches to multiplying each component in a sublist by its list. This errand includes iterating over the sublists, accessing the components and their corresponding indices, and performing the multiplication operation. Two common approaches to achieve this are employing a for loop, leveraging list comprehension, and utilizing the NumPy library. Each approach offers it possesses focal points in terms of code simplicity, proficiency, and lucidness. In this article, we'll investigate these two approaches in detail, giving step-by-step algorithms and Python code illustrations. By the conclusion, you'll have a clear understanding of how to multiply sublist components by their indices in Python.

## Multiplying elements of a sublist

• Python gives a few ways to multiply each element in a sublist by its file. One common approach includes employing a for loop. This strategy emphasizes over the sublists and performs the multiplication operation. By using the enumerate function, we are able to get to both the component and its index in each iteration.

• Moreover, the NumPy library offers an effective solution for cluster operations. By converting the sublists into NumPy arrays, we will perform element-wise increases with the recording cluster. This approach rearranges the increased operation and gives optimized performance.

• All two approaches give the required result of increasing each component in a sublist by its list. The choice of approach depends on components such as code readability, productivity, and the requirement for extra array operations. Python's adaptability permits designers to select the foremost appropriate strategy for their particular prerequisites.

## Approach 1 Using a for loop

The primary approach includes employing a for loop to emphasize the sublists and perform the increase operation. Here's the step-by-step algorithm for this approach −

### Algorithm

• Characterize a function, let's call it multiply_by_index, that takes a list of sublists as input

• Make an empty list named results.

• Use for loop to Iterate over each sublist within the input list

• For each sublist, iterate over its elements and multiply each element by its index.

• Return the results list.

### Example

def multiply(sublists):
results = []
for i, sublist in enumerate(sublists):  # Use a different variable name to avoid shadowing the "list" keyword
multiplied_list = []
for j, element in enumerate(sublist):  # Use separate variables for index and element
multiplied_list.append(element * i)  # Multiply element by its index i instead of j
results.append(multiplied_list)
return results

# Example
sublists = [[71, 2, 83], [4, 50, 6], [37, 8, 19]]
#Invoke multiply function and pass its value to the result variable
result = multiply(sublists)
#Finally, print the result
print(result)


### Output

[[0, 0, 0], [4, 50, 6], [74, 16, 38]]


## Approach 2 Using NumPy

The second approach includes utilizing the NumPy library, which gives proficient and helpful array operations in Python. NumPy arrays are similar to lists but offer extra features and optimizations. Here's the algorithm for this approach:

### Algorithm

• Import the NumPy library.

• Characterize a function, multiply_by_index, that takes a list of sublists as input.

• Convert the sublists to NumPy arrays and assign its value to the sublists.

• Increase the sublists with the index array utilizing element-wise multiplication

• Change over the result back to a nested list.

• Return the modified list of sublists.

### Example

#import the required library
import numpy as np
#Create the user defined function
def multiply(sublists):
sublists = np.array(sublists)
indices = np.arange(len(sublists))
multiplied_sublists = sublists * indices[:, np.newaxis]
return multiplied_sublists.tolist()

# Example
sublists = [[18, 2, 83], [4, 15, 6], [7, 98, 9]]
result = multiply(sublists)
print(result)


### Output

[[0, 0, 0], [4, 15, 6], [14, 196, 18]]


## Conclusion

In this article, we investigated two distinctive approaches to multiplying each element in a sublist by its file using Python. We examined algorithms, gave step-by-step enlightening, and displayed the Python code for each approach. Moreover, we illustrated the output of each approach employing a test input.

You will select any of these approaches based on your specific necessities and inclinations. Python's adaptability and wealthy set of libraries make it simple to achieve different assignments productively.

Updated on: 01-Sep-2023

101 Views