Python Check if previous element is smaller in List (original) (raw)

Last Updated : 02 May, 2023

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 :

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).