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
# 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)
OutputThe 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)
OutputThe 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
Similar Reads
Python | Maximum element in tuple list Sometimes, while working with data in form of records, we can have a problem in which we need to find the maximum element of all the records received. This is a very common application that can occur in Data Science domain. Letâs discuss certain ways in which this task can be performed. Method #1: U
6 min read
Python - Elements Maximum till current index in List Given list with elements, extract element if it's the maximum element till current index. Input : test_list = [4, 6, 7, 8] Output : [4, 6, 7, 8] Explanation : All elements are maximum till their index. Input : test_list = [6, 7, 3, 6, 8, 7] Output : [7, 8] Explanation : 7 and 8 are maximum till thei
7 min read
Python | Cropped list Minimum Sometimes, we require to shrink a list by deletion of its certain elements. One of the methods that is employed to perform this particular task is front and rear element deletion and we also desire to find minimum of this list. It is a good utility whose solution can be useful to have. Letâs discuss
4 min read
Python | Positions of maximum element in list Sometimes, while working with Python lists, we can have a problem in which we intend to find the position of maximum element of list. This task is easy and discussed many times. But sometimes, we can have multiple maximum elements and hence multiple maximum positions. Let's discuss a shorthand to ac
3 min read
Python | Maximize alternate element List The problem of getting maximum of a list is quite generic and we might some day face the issue of getting the maximum of alternate elements and get the list of 2 elements containing maximum of alternate elements. Letâs discuss certain ways in which this can be performed. Method #1 : Using list compr
3 min read