- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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 – Extract elements with equal frequency as value
When it is required to extract elements with equal frequency as value, a list comprehension, the ‘count’ method and the ‘set’ operator are used.
Below is a demonstration of the same −
my_list = [4, 1, 8, 6, 2, 4, 1, 3, 2, 4, 4] print("The list is :") print(my_list) my_result = list(set([element for element in my_list if my_list.count(element) == element])) print("The result is :") print(my_result)
The list is : [4, 1, 8, 6, 2, 4, 1, 3, 2, 4, 4] The result is : [2, 4]
A list is defined and displayed on the console.
A list comprehension is used to iterate over the list, and the count of the element is compared to the element.
This converted to a set and then to a list.
This result is assigned to a variable.
This is the output that is displayed on the console.
- Python – Extract tuples with elements in Range
- Python – Restrict Elements Frequency in List
- Python Program to Extract Rows of a matrix with Even frequency Elements
- Array range queries for elements with frequency same as value in C Program?
- Python – Extract String elements from Mixed Matrix
- Python – Extract elements from Ranges in List
- Python – Fractional Frequency of elements in List
- Python – Filter rows with Elements as Multiple of K
- Python – Check if elements index are equal for list elements
- Python Pandas - Extract the frequency object as a string from the DateTimeIndex
- Python – Extract rows with Even length strings
- Python – Extract Row with any Boolean True
- Python – Extract rows with Complex data types
- Matplotlib – Make a Frequency histogram from a list with tuple elements in Python
- Python – Extract Sorted Strings