

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python – Find the frequency of numbers greater than each element in a list
When it is required to find the frequency of numbers greater than each element in a list, a list comprehension and the ‘sum’ method is used.
Below is a demonstration of the same −
Example
my_list = [24, 13, 72, 22, 12, 47] print("The list is :") print(my_list) my_result = [sum(1 for element in my_list if index <= element) for index in my_list] print("The result is :") print(my_result)
Output
The list is : [24, 13, 72, 22, 12, 47] The result is : [3, 5, 1, 4, 6, 2]
Explanation
A list of integers is defined and is displayed on the console.
A list comprehension is used to iterate over the elements of the list, and check if the index of every element is less than the element itself.
If so, it is added using the ‘sum’ method, converted to a list, and assigned to a variable.
This is displayed as output on the console.
- Related Questions & Answers
- Find smallest element greater than K in Python
- Python Indices of numbers greater than K
- Python Program to find the cube of each list element
- Python - Get the Index of first element greater than K
- Python - Number of values greater than K in list
- Find frequency of each element in a limited range array in less than O(n) time in C++
- Getting equal or greater than number from the list of numbers in JavaScript
- Find frequency of each word in a string in Python
- Find element in a sorted array whose frequency is greater than or equal to n/2 in C++.
- Write a Golang program to find the frequency of each element in an array
- Check if frequency of each digit is less than the digit in Python
- How to find the frequency of values greater than or equal to a certain value in R?
- Python - Find words greater than given length
- Find Smallest Letter Greater Than Target in Python
- Find the element having different frequency than other array elements in C++
Advertisements