
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python - Unique values count of each Key
When it is required to find unique values count of every key, an iteration along with the ‘append’ method is used.
Example
Below is a demonstration of the same
my_list = [12, 33, 33, 54, 84, 16, 16, 16, 58] print("The list is :") print(my_list) filtered_list = [] elem_count = 0 for item in my_list: if item not in filtered_list: elem_count += 1 filtered_list.append(item) print("The result is :") print(elem_count)
Output
The list is : [12, 33, 33, 54, 84, 16, 16, 16, 58] The result is : 6
Explanation
A list is defined and is displayed on the console.
An empty list is defined,
An integer is assigned to 0.
The original list is iterated over.
If an element present in the original list is not present in the second list, the integer is incremented by 1.
The number is appended to the empty list.
This is the output that is displayed on the console.
- Related Articles
- Count unique values per groups in Python Pandas
- Count by unique key in JavaScript
- Python Pandas - Display unique values present in each column
- MySQL Query to get count of unique values?
- Primary key Vs Unique key
- Count unique sublists within list in Python
- Difference between Primary Key and Unique key
- Unique Key in RDBMS
- How to count number of distinct values per field/ key in MongoDB?
- Extract Unique dictionary values in Python Program
- Program to count number of unique binary search tree can be formed with 0 to n values in Python
- Python - Unique keys count for Value in Tuple List
- Get unique values from a list in Python
- Python Pandas - Return unique values in the index
- Python Pandas – Find unique values from multiple columns

Advertisements