- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 −
Example
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)
Output
The list is : [4, 1, 8, 6, 2, 4, 1, 3, 2, 4, 4] The result is : [2, 4]
Explanation
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.
- Related Articles
- 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 Pandas - Extract the frequency object as a string from the DateTimeIndex
- Python – Extract tuples with elements in Range
- Delete elements with frequency atmost K in Python
- Python program to extract rows with common difference elements
- Python Pandas - Extract the minute from DateTimeIndex with specific time series frequency
- Python Pandas - Extract year from the DateTimeIndex with specific time series frequency
- Python Pandas - Extract the frequency from the DateTimeIndex
- Python Pandas - Extract the hour from the DateTimeIndex with specific time series frequency
- Python Pandas - Extract the seconds from the DateTimeIndex with specific time series frequency
- Python Pandas - Extract the microseconds from the DateTimeIndex with specific time series frequency
- Python Pandas - Extract the nanoseconds from the DateTimeIndex with specific time series frequency
- Python Pandas - Extract the timezone from the DateTimeIndex with specific time series frequency
- Python Pandas - Extract month number from the DateTimeIndex with specific time series frequency

Advertisements