Friday, October 10, 2025
HomeLanguagesPython – Ranged Maximum Element in String List

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 certain ways in which this task can be performed.

Input : test_list = [’34, 78, 98, 23, 12′, ’76, 65, 54, 43, 21′] i, j = 1, 3 Output : 76 Input : test_list = [’34, 78, 98, 23, 12′, ’76, 65, 54, 43, 21′] i, j = 3, 5 Output : 98

Method #1 : Using max() + split() + list comprehension The combination of above functions is used to solve this problem. In this, we perform the split of each strings element in list, in a particular range, and then max() is used to find the maximum element in that range across each list. First, maximum amongst the sublist and then amongst the other indices. 

Python3




# Python3 code to demonstrate working of
# Ranged Maximum Element in String Matrix
# Using max() + split() + list comprehension
 
# initializing list
test_list = ['34, 78, 98, 23, 12',
             '76, 65, 54, 43, 21',
             '82, 45, 32, 45, 32',
             '78, 34, 12, 34, 10']
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing Range
i, j = 2, 4
 
# Ranged Maximum Element in String Matrix
# Using max() + split() + list comprehension
res = max([max(idx.split(', ')[i - 1: j]) for idx in test_list])
 
# printing result
print("The maximum ranged element : " + str(res))


Output : 

The original list is : [’34, 78, 98, 23, 12′, ’76, 65, 54, 43, 21′, ’82, 45, 32, 45, 32′, ’78, 34, 12, 34, 10′] The maximum ranged element : 98

Time Complexity: O(n*n) where n is the number of elements in the in the list “test_list”. The max() + split() + 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 in the list “test_list”.

  Method #2 : Using generator expression + max() The combination of above functionalities can be used to solve this problem. In this, we extract all the elements maximum using single max() function and use nested generator expression to extract all elements for strings as once. 

Python3




# Python3 code to demonstrate working of
# Ranged Maximum Element in String Matrix
# Using generator expression + max()
 
# initializing list
test_list = ['34, 78, 98, 23, 12',
             '76, 65, 54, 43, 21',
             '82, 45, 32, 45, 32',
             '78, 34, 12, 34, 10']
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing Range
i, j = 2, 4
 
# Ranged Maximum Element in String Matrix
# Using generator expression + max()
res = max(ele for sub in test_list for ele in sub.split(', ')[i - 1: j])
 
# printing result
print("The maximum ranged element : " + str(res))


Output : 

The original list is : [’34, 78, 98, 23, 12′, ’76, 65, 54, 43, 21′, ’82, 45, 32, 45, 32′, ’78, 34, 12, 34, 10′] The maximum ranged element : 98

Method #3: Using nested for loops

Initialize a variable max_val to -1, which will store the maximum value found in the given range.
Use two nested for loops to iterate over each element in the range (i to j) of each row of the list.
Convert each element to an integer using the int() function and compare it with max_val. If it is greater than max_val, update max_val.
Return max_val as the maximum ranged element.

Python3




# Python3 code to demonstrate working of
# Ranged Maximum Element in String Matrix
# Using nested for loops
 
# initializing list
test_list = ['34, 78, 98, 23, 12',
             '76, 65, 54, 43, 21',
             '82, 45, 32, 45, 32',
             '78, 34, 12, 34, 10']
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing Range
i, j = 2, 4
 
# Ranged Maximum Element in String Matrix
# Using nested for loops
max_val = -1
for row in test_list:
    row_vals = row.split(', ')[i-1:j]
    for val in row_vals:
        if int(val) > max_val:
            max_val = int(val)
 
# printing result
print("The maximum ranged element : " + str(max_val))


Output

The original list is : ['34, 78, 98, 23, 12', '76, 65, 54, 43, 21', '82, 45, 32, 45, 32', '78, 34, 12, 34, 10']
The maximum ranged element : 98

Time complexity: O(n*m), where n is the number of rows and m is the number of columns in the matrix.
Auxiliary space: O(1), as we are only storing a single variable to keep track of the maximum value.

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32349 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6715 POSTS0 COMMENTS
Nicole Veronica
11878 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6837 POSTS0 COMMENTS
Ted Musemwa
7097 POSTS0 COMMENTS
Thapelo Manthata
6792 POSTS0 COMMENTS
Umr Jansen
6791 POSTS0 COMMENTS