One of the problems that is basically a subproblem for many complex problems, finding numbers greater than a certain number in a list in python, is commonly encountered and this particular article discusses possible solutions to this particular problem.
Method 1 : Naive method The most common way this problem can be solved is using loop and just counting the occurrences of elements that are greater than the given number K.
Python3
test_list = [ 1 , 7 , 5 , 6 , 3 , 8 ]
k = 4
print ( "The list : " + str (test_list))
count = 0
for i in test_list:
if i > k:
count = count + 1
print ( "The numbers greater than 4 : " + str (count))
|
Output :
The list : [1, 7, 5, 6, 3, 8]
The numbers greater than 4 : 4
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 2 : Using list comprehension This method achieves this task in similar way, but in more concise manner. List comprehension always lower the lines of codes in the program even though runs a similar approach in background.
Python3
test_list = [ 1 , 7 , 5 , 6 , 3 , 8 ]
k = 4
print ( "The list : " + str (test_list))
count = len ([i for i in test_list if i > k])
print ( "The numbers greater than 4 : " + str (count))
|
Output :
The list : [1, 7, 5, 6, 3, 8]
The numbers greater than 4 : 4
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 3: Using sum() The sum() can also help us achieving this task. We can return 1 when the number greater than k is found and then compute the summation of is using sum()
Python3
test_list = [ 1 , 7 , 5 , 6 , 3 , 8 ]
k = 4
print ( "The list : " + str (test_list))
count = sum (i > k for i in test_list)
print ( "The numbers greater than 4 : " + str (count))
|
Output :
The list : [1, 7, 5, 6, 3, 8]
The numbers greater than 4 : 4
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 4: Using functools.reduce() By using reduce(), we can also perform the summation of all the collected numbers for the function and then accumulate them to return the result i.e the count of numbers greater than K.
Python3
from functools import reduce
test_list = [ 1 , 7 , 5 , 6 , 3 , 8 ]
k = 4
print ( "The list : " + str (test_list))
count = reduce ( lambda sum , j: sum + ( 1 if j > k else 0 ), test_list, 0 )
print ( "The numbers greater than 4 : " + str (count))
|
Output :
The list : [1, 7, 5, 6, 3, 8]
The numbers greater than 4 : 4
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 5 : Using bisect() + sort() The combination of sort() and bisect(), can actually perform the task of binary search, and hence getting the index and subtracting the size of list can actually help us get elements that are greater than particular element in the list.
Python3
from bisect import bisect
test_list = [ 1 , 7 , 5 , 6 , 3 , 8 ]
k = 4
print ( "The list : " + str (test_list))
test_list.sort()
count = len (test_list) - bisect(test_list, k)
print ( "The numbers greater than 4 : " + str (count))
|
Output :
The list : [1, 7, 5, 6, 3, 8]
The numbers greater than 4 : 4
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #6: Using filter(),list(),len() and lambda functions
Python3
test_list = [ 1 , 7 , 5 , 6 , 3 , 8 ]
k = 4
print ( "The list : " + str (test_list))
result = list ( filter ( lambda x: x > k, test_list))
print ( "The numbers greater than 4 : " + str ( len (result)))
|
OutputThe list : [1, 7, 5, 6, 3, 8]
The numbers greater than 4 : 4
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #7: Using remove() function
Python3
test_list = [ 1 , 7 , 5 , 6 , 3 , 8 ]
k = 4
print ( "The list : " + str (test_list))
for i in test_list.copy():
if i < k:
test_list.remove(i)
print ( "The numbers greater than 4 : " + str ( len (test_list)))
|
OutputThe list : [1, 7, 5, 6, 3, 8]
The numbers greater than 4 : 4
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #8: Using numpy():
Python3
import numpy as np
test_list = [ 1 , 7 , 5 , 6 , 3 , 8 ]
print ( "The list : " + str (test_list))
count = np. sum (np.array(test_list) > 4 )
print ( "The numbers greater than 4:" , count)
|
Output:
The list : [1, 7, 5, 6, 3, 8]
The numbers greater than 4: 4
Time Complexity: O(n)
Auxiliary Space: O(n)