Sometimes, while working with data, we can have a problem in which we receive a series of lists with data in string format, which we wish to find the product of each string list integer. Let’s discuss certain ways in which this task can be performed.
Method #1: Using loop + int()
This is the brute force method to perform this task. In this, we run a loop for the entire list, convert each string to an integer and perform product listwise and store it in a separate list.
Python3
# Python3 code to demonstrate working of # String Integer Product # using loop + int() # getting Product def prod(val) : res = 1 for ele in val: res * = int (ele) return res # initialize list test_list = [[ '1' , '4' ], [ '5' , '6' ], [ '7' , '10' ]] # printing original list print ("The original list : " + str (test_list)) # String Integer Product # using loop + int() res = [] for sub in test_list: par_prod = prod(sub) res.append(par_prod) # printing result print (" List after product of nested string lists : " + str (res)) |
The original list : [['1', '4'], ['5', '6'], ['7', '10']] List after product of nested string lists : [4, 30, 70]
Time Complexity: O(n^2) where n is the length of the test_list
Auxiliary Space: O(n) where n is the length of the res list.
Method #2: Using loop + int() + list comprehension
This is the shorthand with the help of which this task can be performed. In this, we run a loop on lists using list comprehension and extract product using explicit product function.
Python3
# Python3 code to demonstrate working of # String Integer Product # using loop + int() + list comprehension # getting Product def prod(val) : res = 1 for ele in val: res * = int (ele) return res # initialize list test_list = [[ '1' , '4' ], [ '5' , '6' ], [ '7' , '10' ]] # printing original list print ("The original list : " + str (test_list)) # String Integer Product # using loop + int() + list comprehension res = [prod(sub) for sub in test_list] # printing result print (" List after product of nested string lists : " + str (res)) |
The original list : [['1', '4'], ['5', '6'], ['7', '10']] List after product of nested string lists : [4, 30, 70]
Time Complexity: O(n^2) where n is the length of the test_list
Auxiliary Space: O(n) where n is the length of the res list.
Method #3 : Using list(),map(),functors.reduce() and operator.mul
Python3
# Python3 code to demonstrate working of # String Integer Product # initialize list test_list = [[ '1' , '4' ], [ '5' , '6' ], [ '7' , '10' ]] # printing original list print ( "The original list : " + str (test_list)) # String Integer Product res = [] for sub in test_list: x = list ( map ( int ,sub)) from functools import reduce import operator a = reduce (operator.mul,x, 1 ) res.append(a) # printing result print ( "List after product of nested string lists : " + str (res)) |
The original list : [['1', '4'], ['5', '6'], ['7', '10']] List after product of nested string lists : [4, 30, 70]
Time Complexity: O(n*n), where n is the length of the input list. This is because we’re using list(),map(),functors.reduce() and operator.mul which has a time complexity of O(n*n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list
Method #4 : Using numpy.prod()
This task can be accomplished using numpy library, by using numpy.prod() method which performs product of elements of an array.
Note: Install numpy module using command “pip install numpy”
Python3
# Python3 code to demonstrate working of # String Integer Product # Using numpy.prod() # importing numpy import numpy as np # initialize list test_list = [[ '1' , '4' ], [ '5' , '6' ], [ '7' , '10' ]] # printing original list print ( "The original list : " + str (test_list)) # String Integer Product # using numpy.prod() res = [np.prod([ int (y) for y in x]) for x in test_list] # printing result print ( "List after product of nested string lists : " + str (res)) #This code is contributed by Edula Vinay Kumar Reddy |
Output :
The original list : [['1', '4'], ['5', '6'], ['7', '10']] List after product of nested string lists : [4, 30, 70]
Time Complexity: The time complexity is O(N) where N is the number of elements in the list.
Space Complexity: The space complexity is O(N) where N is the number of elements in the list.