Skip to content
Related Articles
Python – Ranged Maximum Element in String List
• Last Updated : 02 Jun, 2020

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

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

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up