Python - Maximum of String Integer list
Last Updated :
09 Apr, 2023
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
# Python3 code to demonstrate working of
# Maximum of String Integer
# using loop + int()
# initialize list
test_list = [['1', '4'], ['5', '6'], ['7', '10']]
# printing original list
print("The original list : " + str(test_list))
# Maximum of String Integer
# using loop + int()
res = []
for sub in test_list:
par_max = 0
for ele in sub:
par_max = max(par_max, int(ele))
res.append(par_max)
# printing result
print("List after maximization of nested string lists : " + str(res))
OutputThe 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
# Python3 code to demonstrate working of
# Maximum of String Integer
# using max() + int() + list comprehension
# initialize list
test_list = [['1', '4'], ['5', '6'], ['7', '10']]
# printing original list
print("The original list : " + str(test_list))
# Maximum of String Integer
# using max() + int() + list comprehension
res = [max(int(ele) for ele in sub) for sub in test_list]
# printing result
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
# Python3 code to demonstrate working of
# Maximum of String Integer
# using map() and max()
# initialize list
test_list = [['1', '4'], ['5', '6'], ['7', '10']]
# printing original list
print("The original list : " + str(test_list))
# Maximum of String Integer
# using map() and max()
res = [max(map(int, sub)) for sub in test_list]
# printing result
print("List after maximization of nested string lists : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy
OutputThe 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))
#This code is contributed by pinjala Jyothi.
Time Complexity: O(N)
Auxiliary Space : O(N)
Similar Reads
List of strings in Python
A list of strings in Python stores multiple strings together. In this article, weâll explore how to create, modify and work with lists of strings using simple examples.Creating a List of StringsWe can use square brackets [] and separate each string with a comma to create a list of strings.Pythona =
2 min read
Python | Maximum Difference in String
Sometimes, we might have a problem in which we require to get the maximum difference of 2 numbers from Strings but with a constraint of having the numbers in successions. This type of problem can occur while competitive programming. Letâs discuss certain ways in which this problem can be solved. Met
4 min read
Maximum String Value Length of Key - Python
The task of finding the maximum string length of a specific key in a list of dictionaries involves identifying the longest string associated with the given key. Given a list of dictionaries, the goal is to extract the string values of the specified key, compute their lengths and determine the maximu
3 min read
Python - Maximum occurring Substring from list
Sometimes, while working with Python strings, we can have a problem in which we need to check for maximum occurring substring from strings list. This can have application in DNA sequencing in Biology and other application. Let's discuss certain way in which this task can be performed. Method 1 : Usi
6 min read
Python - Maximum Quotient Pair in List
Sometimes, we need to find the specific problem of getting the pair which yields the maximum Quotient, this can be solved by sorting and getting the first and last elements of the list. But in some case, we donât with to change the ordering of list and perform some operation in a similar list withou
5 min read
Python - Ranged Maximum Element in String List
Sometimes, while working with Python data, we can have a problem in which we have data in form of String List and we require to find the maximum element in that data, but that also in a certain range of indices. This is quite peculiar problem but can have application in data domains. Let's discuss c
4 min read
Longest String in list - Python
We are given a list of strings, and our task is to find the longest string present in the list. If there are multiple strings with the maximum length, we should return the first one that appears. For example, given a = ["alpha", "gamma", "epsilon", "delta"], the longest string is "epsilon". Let's di
2 min read
Python - Maximum of Consecutive Pair in integer list
Sometimes, while working with Python list, one can have a problem in which one needs to find perform the maximization of list in pair form. This is useful as a subproblem solution of bigger problem in web development and day-day programming. Letâs discuss certain ways in which this problem can be so
5 min read
Python - Integers String to Integer List
In this article, we will check How we can Convert an Integer String to an Integer List Using split() and map()Using split() and map() will allow us to split a string into individual elements and then apply a function to each element. Pythons = "1 2 3 4 5" # Convert the string to a list of integers u
2 min read
Python - Records Maxima in List of Tuples
Sometimes, while working with records, we can have a problem in which we need to the maximum all the columns of a container of lists that are tuples. This kind of application is common in the web development domain. Letâs discuss certain ways in which this task can be performed. Method #1 : Using ma
5 min read