Python | Number of values greater than K in list

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 methodThe 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 listtest_list = [1, 7, 5, 6, 3, 8]  # initializing kk = 4  # printing list print ("The list : " + str(test_list))  # using naive method # to get numbers > kcount = 0for 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 comprehensionThis 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 listtest_list = [1, 7, 5, 6, 3, 8]  # initializing kk = 4  # printing list print ("The list : " + str(test_list))  # using list comprehension# to get numbers > kcount = 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 listtest_list = [1, 7, 5, 6, 3, 8]  # initializing kk = 4  # printing list print ("The list : " + str(test_list))  # using sum()# to get numbers > kcount = 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 listtest_list = [1, 7, 5, 6, 3, 8]  # initializing kk = 4  # printing list print ("The list : " + str(test_list))  # using reduce()# to get numbers > kcount = 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 listtest_list = [1, 7, 5, 6, 3, 8]  # initializing kk = 4  # printing list print ("The list : " + str(test_list))  # using bisect() + sort()# to get numbers > ktest_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

Python list-programs
python-list
Python
python-list
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Read a file line by line in Python
How to Install PIP on Windows ?
Python String | replace()
*args and **kwargs in Python
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON
Python | os.path.join() method