Python | Number of values greater than K in list

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.