Python – Maximum of String Integer list
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 max 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 integer and perform maximization listwise and store in a separate list.
Python3
test_list = [[ '1' , '4' ], [ '5' , '6' ], [ '7' , '10' ]]
print ( "The original list : " + str (test_list))
res = []
for sub in test_list:
par_max = 0
for ele in sub:
par_max = max (par_max, int (ele))
res.append(par_max)
print ( "List after maximization of nested string lists : " + str (res))
|
Output
The original list : [['1', '4'], ['5', '6'], ['7', '10']]
List after maximization of nested string lists : [4, 6, 10]
Time Complexity: O(n*n), where n is the length of the input list. This is because we’re using the loop + int() 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 #2 : Using max() + 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 maximization using max().
Python3
test_list = [[ '1' , '4' ], [ '5' , '6' ], [ '7' , '10' ]]
print ( "The original list : " + str (test_list))
res = [ max ( int (ele) for ele in sub) for sub in test_list]
print ( "List after maximization of nested string lists : " + str (res))
|
Output :
The original list : [['1', '4'], ['5', '6'], ['7', '10']]
List after maximization of nested string lists : [4, 6, 10]
Time Complexity: O(n*n) where n is the number of elements in the string list. The max() + int() + list comprehension is used to perform the task and it takes O(n*n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res test_list.
Method #3 : Using map()
Another approach that can be used to find the maximum of a list of string integers is by using the map() function to convert the strings to integers, and then using the max() function to find the maximum value.
Python3
test_list = [[ '1' , '4' ], [ '5' , '6' ], [ '7' , '10' ]]
print ( "The original list : " + str (test_list))
res = [ max ( map ( int , sub)) for sub in test_list]
print ( "List after maximization of nested string lists : " + str (res))
|
Output
The original list : [['1', '4'], ['5', '6'], ['7', '10']]
List after maximization of nested string lists : [4, 6, 10]
The time and Auxiliary space of this approach is O(n) where n is the number of elements in the list.
Method #4 : Using map() and lambda function:
Python3
test_list = [[ '1' , '4' ], [ '5' , '6' ], [ '7' , '10' ]]
res = list ( map ( lambda x: max ( map ( int , x)), test_list))
print ( "List after maximization of nested string lists : " + str (res))
|
Time Complexity: O(N)
Auxiliary Space : O(N)
Last Updated :
09 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...