Open In App

Python – Check if previous element is smaller in List

Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python lists, we can have a problem in which we need to check for each element if its preceding element is smaller. This type of problem can have its use in data preprocessing domains. Let’s discuss certain problems in which this task can be performed.

Input : test_list = [1, 3, 5, 6, 8] 
Output : [True, True, True, True]

Input : test_list = [3, 1] 
Output : [False] 

Method #1 : Using loop 
This is one of the ways in which this task can be performed. In this, we perform the task of checking for elements using brute for in loop.

Python3




# Python3 code to demonstrate working of
# Check if previous element is smaller in List
# Using loop
 
# initializing list
test_list = [6, 3, 7, 3, 6, 7, 8, 3]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Check if previous element is smaller in List
# Using loop
res = []
for idx in range(1, len(test_list)):
    if test_list[idx - 1] < test_list[idx]:
        res.append(True)
    else:
        res.append(False)
 
# printing result
print("List after filtering : " + str(res))


Output : 

The original list is : [6, 3, 7, 3, 6, 7, 8, 3]
List after filtering : [False, True, False, True, True, True, False]

 

Time complexity: O(n), where n is the length of the input list.
Auxiliary Space: O(n), where n is the length of the input list. This is because the program creates a list of the same length as the input list to store the results.

Method #2 : Using zip() + list comprehension 
This is one-liner approach to solve this problem. In this, we first, zip the list and its next element list, and then check for comparisons for result.

Python3




# Python3 code to demonstrate working of
# Check if previous element is smaller in List
# Using zip() + list comprehension
 
# initializing list
test_list = [6, 3, 7, 3, 6, 7, 8, 3]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Check if previous element is smaller in List
# Using zip() + list comprehension
res = [int(sub1) < int(sub2) for sub1, sub2 in zip(test_list, test_list[1:])]
 
# printing result
print("List after filtering : " + str(res))


Output : 

The original list is : [6, 3, 7, 3, 6, 7, 8, 3]
List after filtering : [False, True, False, True, True, True, False]

 

The time complexity of the given code is O(n), where n is the length of the input list.
The auxiliary space complexity of the given code is O(n),

Method 3 : Using the built-in map() function 

Steps : 

  • The first line imports the itertools module which provides various functions to work with iterators and combinatorial iterators.
  • The second line initializes a list named “test_list” with some values.
  • The third line prints the original list using the print() function.
  • The fourth line uses the map() function to check if the previous element is smaller in the list.
  • The map() function applies a lambda function to each element of the iterable object. In this case, it applies the lambda function to each pair of adjacent elements in the list using the zip() function. The lambda function returns True if the first element of the pair is greater than the second element, otherwise it returns False.
  • The zip() function takes two or more iterables and returns an iterator of tuples containing elements from each iterable. In this case, it takes the test_list from the second element to the end (test_list[1:]) and the test_list from the beginning to the second to last element (test_list[:-1]) and returns an iterator of tuples containing pairs of adjacent elements in the original list.
  • The result of the map() function is a map object, which is an iterator that applies the lambda function to each element of the iterable object.
  • The result is converted to a list using the list() function and stored in the variable “res”.
  • The last line prints the filtered list using the print() function

Python3




# Python3 code to demonstrate working of
# Check if previous element is smaller in List
# Using map() function
 
# initializing list
test_list = [6, 3, 7, 3, 6, 7, 8, 3]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Check if previous element is smaller in List
# Using map() function
res = list(map(lambda i: i[0] > i[1], zip(test_list[1:], test_list)))
 
# printing result
print("List after filtering : " + str(res))


Output

The original list is : [6, 3, 7, 3, 6, 7, 8, 3]
List after filtering : [False, True, False, True, True, True, False]

Time complexity: The zip() function and the map() function both iterate over the list once, so the time complexity of this approach is O(n).
Auxiliary space: We create a new list to store the filtered values, so the auxiliary space complexity of this approach is O(n).



Last Updated : 02 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads