Sunday, November 17, 2024
Google search engine
HomeLanguagesPython | Multiply each element in a sublist by its index

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




# 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]

Time complexity: O(n^2)
Auxiliary space: O(n)

Method #2: Using List comprehension 

Python3




# 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]

Time complexity: O(nm), where n is the number of sublists in the input list and m is the length of each sublist.
Auxiliary space: O(n), where n is the number of sublists in the input list. This is because the program uses list comprehension to create a new list of the same length as the input list.

Method #3: Using numpy 

Python3




# 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]

Time Complexity: The time complexity of the given code is O(n^2), where n is the number of elements in the list.

Auxiliary Space: The auxiliary space complexity of the code is O(n), where n is the number of elements in the list.

Method #4: Using Zip and List Comprehension

We can use the zip() function to solve the problem of multiplying each element in a sublist by its index and returning a summed list. We can use the range() function to generate the indices for each element in the sublist, and use the zip() function to pair each element with its index. Then, we can use a list comprehension to compute the sum of the products of the indices and elements.

Python3




# Python3 code to demonstrate
# to multiply numbers with position
# and add them to return num
 
# initializing list
ini_list = [[3, 4, 7], [6, 7, 8], [10, 7, 5], [11, 12, 13]]
 
# printing initial_list
print("initial_list", ini_list)
 
# using zip and list comprehension
res = [sum(i * j for i, j in zip(range(len(sublist)), sublist))
       for sublist in ini_list]
 
# printing result
print("result", res)
# This code is contributed by Edula Vinay Kumar Reddy


Output

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

Time Complexity: The time complexity of this approach is O(n*m) where n is the number of sublists in the initial list and m is the length of each sublist. This is because we are iterating through each sublist and then iterating through each element of the sublist to calculate the dot product.
Auxiliary Space: The auxiliary space of this approach is O(n) where n is the number of sublists in the initial list. This is because we are creating a new list (res) to store the dot product of each sublist.

RELATED ARTICLES

Most Popular

Recent Comments