Python – Maximum element in Cropped List
Last Updated :
04 May, 2023
Sometimes, while working with Python, we can have a problem in which we need to get maximum of list. But sometimes, we need to get this for between custom indices. This can be need of any domain be it normal programming or web development. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using loop + max() This is brute force method in which we performed this task. In this, we just add to new list the elements in specified range. Then max() is used to compute maximum.
Python3
test_list = [ 2 , 3 , 5 , 7 , 9 , 10 , 8 , 6 ]
print ("The original list is : " + str (test_list))
i, j = 2 , 5
res = []
for idx, ele in enumerate (test_list):
if idx > = i and idx < j:
res.append(ele)
res = max (res)
print ("The maximum element in range is : " + str (res))
|
Output :
The original list is : [2, 3, 5, 7, 9, 10, 8, 6]
The maximum element in range is : 9
Time Complexity: O(n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the new res list
Method #2 : Using list slicing + max() The combination of above functions can be used to perform this task. In this, we just perform slicing using list slicing and max() performs the task of extracting max.
Python3
test_list = [ 2 , 3 , 5 , 7 , 9 , 10 , 8 , 6 ]
print ("The original list is : " + str (test_list))
i, j = 2 , 5
res = test_list[i : j]
res = max (res)
print ("The maximum element in range is : " + str (res))
|
Output :
The original list is : [2, 3, 5, 7, 9, 10, 8, 6]
The maximum element in range is : 9
Method #3 : Using itertools
Here’s another approach to solve the problem using the islice function from the itertools module:
Python3
from itertools import islice
test_list = [ 2 , 3 , 5 , 7 , 9 , 10 , 8 , 6 ]
print ( "The original list is:" , test_list)
i, j = 2 , 5
res = max (islice(test_list, i, j))
print ( "The maximum element in the range is:" , res)
|
Output
The original list is: [2, 3, 5, 7, 9, 10, 8, 6]
The maximum element in the range is: 9
Time complexity: O(n) (where n is the length of the list), since islice iterates over n elements of the list.
Space complexity: O(1) since islice only uses a constant amount of memory to store pointers to the start and end of the slice, and the maximum value found so far.
Method #4 : Using the built-in sorted() function
- Initialize the test list
- Print the original list
- Initialize variables i and j with the indices of the desired cropped list
- Use the sorted() function to create a sorted list of the cropped elements
- Assign the last element of the sorted list to a variable max_val
- Print the maximum element in the range using the max_val variable
Python3
test_list = [ 2 , 3 , 5 , 7 , 9 , 10 , 8 , 6 ]
print ( "The original list is:" , test_list)
i, j = 2 , 5
max_val = sorted (test_list[i:j])[ - 1 ]
print ( "The maximum element in the range is:" , max_val)
|
Output
The original list is: [2, 3, 5, 7, 9, 10, 8, 6]
The maximum element in the range is: 9
Time complexity: O(nlogn) due to the sorting operation
Auxiliary space: O(n) due to the creation of the sorted list
Share your thoughts in the comments
Please Login to comment...