Open In App

Python – Maximum element in Cropped List

Last Updated : 04 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate
# Maximum element in Cropped List
# using loop + max()
 
# initializing list
test_list = [2, 3, 5, 7, 9, 10, 8, 6]
 
# printing original list
print ("The original list is : " + str(test_list))
 
i, j = 2, 5
 
# Maximum element in Cropped List
# using loop + max()
res = []
for idx, ele in enumerate(test_list):
    if idx >= i and idx < j:
        res.append(ele)
res = max(res)
 
# printing result
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




# Python3 code to demonstrate
# Maximum element in Cropped List
# using list slicing + max()
 
# initializing list
test_list = [2, 3, 5, 7, 9, 10, 8, 6]
 
# printing original list
print ("The original list is : " + str(test_list))
 
i, j = 2, 5
 
# Maximum element in Cropped List
# using list slicing + max()
res = test_list[i : j]
res = max(res)
 
# printing result
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




# Using islice to find maximum element in a cropped list
from itertools import islice
 
# Initialize the test list
test_list = [2, 3, 5, 7, 9, 10, 8, 6]
 
# Print original list
print("The original list is:", test_list)
 
# Indices of the desired cropped list
i, j = 2, 5
 
# Use islice to create a cropped list, then use max to find the maximum element
res = max(islice(test_list, i, j))
 
# Print result
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




# Using sorted() to find maximum element in a cropped list
 
# Initialize the test list
test_list = [2, 3, 5, 7, 9, 10, 8, 6]
 
# Print original list
print("The original list is:", test_list)
 
# Indices of the desired cropped list
i, j = 2, 5
 
# Use sorted() to create a sorted list of the cropped elements, then assign the last element to max_val
max_val = sorted(test_list[i:j])[-1]
 
# Print result
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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads