Skip to content
Related Articles

Related Articles

Python – Maximum of String Integer list

View Discussion
Improve Article
Save Article
  • Last Updated : 21 Feb, 2022
View Discussion
Improve Article
Save Article

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))

Output : 

The original list : [['1', '4'], ['5', '6'], ['7', '10']]
List after maximization of nested string lists : [4, 6, 10]

 

 
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]

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!