Thursday, December 26, 2024
Google search engine
HomeLanguagesPython | Number of values greater than K in list

Python | Number of values greater than K in list

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




# Python 3 code to demonstrate
# find number of elements > k
# using naive method
 
# initializing list
test_list = [1, 7, 5, 6, 3, 8]
 
# initializing k
k = 4
 
# printing list
print("The list : " + str(test_list))
 
# using naive method
# to get numbers > k
count = 0
for i in test_list:
    if i > k:
        count = count + 1
 
# printing the intersection
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




# Python 3 code to demonstrate
# find number of elements > k
# using list comprehension
 
# initializing list
test_list = [1, 7, 5, 6, 3, 8]
 
# initializing k
k = 4
 
# printing list
print("The list : " + str(test_list))
 
# using list comprehension
# to get numbers > k
count = len([i for i in test_list if i > k])
 
# printing the intersection
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




# Python 3 code to demonstrate
# find number of elements > k
# using sum()
 
# initializing list
test_list = [1, 7, 5, 6, 3, 8]
 
# initializing k
k = 4
 
# printing list
print("The list : " + str(test_list))
 
# using sum()
# to get numbers > k
count = sum(i > k for i in test_list)
 
# printing the intersection
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




# Python 3 code to demonstrate
# find number of elements > k
# using reduce()
from functools import reduce
 
# initializing list
test_list = [1, 7, 5, 6, 3, 8]
 
# initializing k
k = 4
 
# printing list
print("The list : " + str(test_list))
 
# using reduce()
# to get numbers > k
count = reduce(lambda sum, j: sum + (1 if j > k else 0), test_list, 0)
 
# printing the intersection
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




# Python 3 code to demonstrate
# find number of elements > k
# using bisect() + sort()
from bisect import bisect
 
# initializing list
test_list = [1, 7, 5, 6, 3, 8]
 
# initializing k
k = 4
 
# printing list
print("The list : " + str(test_list))
 
# using bisect() + sort()
# to get numbers > k
test_list.sort()
count = len(test_list) - bisect(test_list, k)
 
# printing the intersection
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




# Python 3 code to demonstrate
# find number of elements > k
 
# initializing list
test_list = [1, 7, 5, 6, 3, 8]
 
# initializing k
k = 4
 
# printing list
print("The list : " + str(test_list))
 
result = list(filter(lambda x: x > k, test_list))
 
# printing the intersection
print("The numbers greater than 4 : " + str(len(result)))


Output

The 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




# Python 3 code to demonstrate
# find number of elements > k
 
# initializing list
test_list = [1, 7, 5, 6, 3, 8]
 
# initializing k
k = 4
 
# printing list
print("The list : " + str(test_list))
 
for i in test_list.copy():
    if i < k:
        test_list.remove(i)
 
# printing the intersection
print("The numbers greater than 4 : " + str(len(test_list)))


Output

The 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]
# printing list
print("The list : " + str(test_list))
count = np.sum(np.array(test_list) > 4)
print("The numbers greater than 4:", count)
#this code is contributed by Jyothi pinjala.


Output:

The list : [1, 7, 5, 6, 3, 8]
The numbers greater than 4: 4
 

Time Complexity: O(n)
Auxiliary Space: O(n)

RELATED ARTICLES

Most Popular

Recent Comments