Skip to content
Related Articles

Related Articles

Improve Article

Python | Number of values greater than K in list

  • Last Updated : 25 Nov, 2018

One of the problem that is basically a subproblem for many complex problem, finding numbers greater than certain number in 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.




# 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

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.




# 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

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




# 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

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.




# 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

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.




# 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

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :