Python - Remove non-increasing elements
Last Updated :
08 Mar, 2023
Given a list, our task is to write a Python program to remove all the non-increasing elements from the list.
Input : test_list = [5, 3, 4, 5, 7, 3, 9, 10, 3, 10, 12, 13, 3, 16, 1]
Output : [5, 5, 5, 7, 9, 10, 10, 12, 13, 16]
Explanation : 3, 4 are omitted as 5, (greater element) had occurred before them. Applies to all other omitted elements.
Input : test_list = [5, 3, 4, 5, 7, 3, 9]
Output : [5, 5, 5, 7, 9]
Explanation : 3, 4 are omitted as 5, (greater element) had occurred before them. Applies to all other omitted elements.
Method #1 : Using loop
In this, the previous element is checked before populating the further list, if the greater element is found, it's appended, else it's dropped using loop.
Python3
# Python3 code to demonstrate working of
# Remove non-increasing elements
# Using loop
# initializing list
test_list = [5, 3, 4, 5, 7, 3, 9, 10, 3, 10, 12, 13, 3, 16, 1]
# printing original list
print("The original list is : " + str(test_list))
res = [test_list[0]]
for ele in test_list:
# checking preceding element to decide for greater element
if ele >= res[-1]:
res.append(ele)
# printing result
print("The list after removing non-increasing elements : " + str(res))
Output:
The original list is : [5, 3, 4, 5, 7, 3, 9, 10, 3, 10, 12, 13, 3, 16, 1]
The list after removing non-increasing elements : [5, 5, 5, 7, 9, 10, 10, 12, 13, 16]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using list comprehension + max + zip() + accumulate
In this, maximum elements are zipped till the current element, and then list comprehension is used to check if a higher element occurs than the current, if yes it's added, else the new element is omitted from the result in runtime iteration.
Python3
# Python3 code to demonstrate working of
# Remove non-increasing elements
# Using list comprehension + max + zip() + accumulate
from itertools import accumulate
# initializing list
test_list = [5, 3, 4, 5, 7, 3, 9, 10, 3, 10, 12, 13, 3, 16, 1]
# printing original list
print("The original list is : " + str(test_list))
# checking for each element with curr maximum computed using zip
res = [idx for idx, ele in zip(test_list, accumulate(test_list, max)) if idx == ele]
# printing result
print("The list after removing non-increasing elements : " + str(res))
Output:
The original list is : [5, 3, 4, 5, 7, 3, 9, 10, 3, 10, 12, 13, 3, 16, 1]
The list after removing non-increasing elements : [5, 5, 5, 7, 9, 10, 10, 12, 13, 16]
Time complexity: O(n*n), where n is the length of the test_list. The list comprehension + max + zip() + accumulate takes O(n*n) time
Auxiliary Space: O(n), extra space of size n is required
Similar Reads
Python | Remove Front K elements We often come to situations in which we need to decrease the size of the list by truncating the first elements of the list. This particular problem occurs when we need to optimize memory. This has its application in the day-day programming when sometimes we require to get all the lists of similar si
7 min read
Remove last K elements of list - Python Given a list and an integer K, the task is to remove the last K elements from the list. For example, if the list is [1, 2, 3, 4, 5] and K = 2, the result should be [1, 2, 3]. Letâs explore different methods to remove the last K elements from a list in Python.Using list slicingList slicing is one of
3 min read
Python - Odd elements removal in List Due to the upcoming of Machine Learning, focus has now moved on handling the certain values than ever before, the reason behind this is that it is the essential step of data preprocessing before it is fed into further techniques to perform. Hence removal of certain values in essential and knowledge
5 min read
Remove elements at Indices in List - Python In Python, lists store items in a specific order and each item has an index. Removing elements by index means creating a new list that excludes items at certain positions. This helps in keeping only the needed elements while discarding others based on their index. For example:Input : li= [5, 6, 3, 7
2 min read
Remove Negative Elements in List-Python The task of removing negative elements from a list in Python involves filtering out all values that are less than zero, leaving only non-negative numbers. Given a list of integers, the goal is to iterate through the elements, check for positivity and construct a new list containing only positive num
3 min read
Python | Remove Initial K column elements Sometimes, while working with Matrix data, we can have stray elements that attached at front end of each row of matrix. This can be undesired at times and wished to be removed. Letâs discuss certain ways in which this task can be performed. Method #1 : Using loop + del + list slicing The combination
4 min read