Count unique items in a list
Last Updated : 06 Oct, 2024
There are several methods for finding or counting unique items inside a list in Python. Here we'll discuss 3 methods.
Brute Force Approach
The first method is the brute force approach.We traverse from the start and check the items. If the item is not in the empty list(as it has taken empty) then we will add it to the empty list and increase the counter by 1. While traveling if the item is in the taken list(empty list) we will not count it.
Python # taking an input list input_list = [1, 2, 2, 5, 8, 4, 4, 8] # taking an input list l1 = [] # taking an counter count = 0 # traversing the array for item in input_list: if item not in l1: count += 1 l1.append(item) # printing the output print("No of unique items are:", count)
Output:
No of unique items are: 5
Time complexity: O(n^2), where n is the length of the list
Auxiliary Space: O(n), extra space of size n is required
Using Counter in Python
In this method, we will use a function name Counter. The module collections have this function. Using the Counter function we will create a dictionary. The keys of the dictionary will be the unique items and the values will be the number of that key present in the list.
Python # importing Counter module from collections import Counter input_list = [1, 2, 2, 5, 8, 4, 4, 8] # creating a list with the keys items = Counter(input_list).keys() print("No of unique items in the list are:", len(items))
Output:
No of unique items in the list are: 5
If we print the length of the dictionary created using Counter will also give us the result. But this method is more understandable.
In this method, we will convert our list to set. As sets don't contain any duplicate items then printing the length of the set will give us the total number of unique items.
Python input_list = [1, 2, 2, 5, 8, 4, 4, 8] # converting our list to set new_set = set(input_list) print("No of unique items in the list are:", len(new_set))
Output:
No of unique items in the list are: 5
Time complexity: O(n), where n is the length of input_list
Auxiliary Space: O(n), extra space required for set.
Using Enumerate
In this method, we will remove all the duplicate from our list. As list don't contain any duplicate items then printing the length of the list will give us the total number of unique items.
Python input_list = [1, 2, 2, 5, 8, 4, 4, 8] # converting our list to filter list new_set = [ x for i, x in enumerate(input_list) if x not in input_list[:i]] print("No of unique items in the list are:", len(new_set))
Output:
No of unique items in the list are: 5
Similar Reads
Python | Integer count in Mixed List The lists in python can handle different type of data types in it. The manipulation of such lists is complicated. Sometimes we have a problem in which we need to find the count of integer values in which the list can contain string as a data type i.e heterogeneous. Letâs discuss certain ways in whic
6 min read
Python | Count unique sublists within list Given a list of lists, the task is to find the count of unique sublists within list. Examples: Input: [['Geek', 'for', 'geeks'], ['geeks', 'for'], ['for', 'Geeks', 'geek'], ['Geek', 'for', 'geeks']] Output: {('geeks', 'for'): 1, ('for', 'Geeks', 'geek'): 1, ('Geek', 'for', 'geeks'): 2} Below are som
2 min read
Counting number of unique values in a Python list Counting the number of unique values in a Python list involves determining how many distinct elements are present disregarding duplicates.Using a SetUsing a set to count the number of unique values in a Python list leverages the property of sets where each element is stored only once.Pythonli = [1,
2 min read
Python - Count Dictionary Items In Python, dictionaries are one of the most widely used data structures. They allow us to store data in key-value pairs where each key maps to a value. In this article, weâll explore how to count dictionary items in Python using different methods including built-in functions and loops.Counting Total
3 min read
Python | Count true booleans in a list Given a list of booleans, write a Python program to find the count of true booleans in the given list. Examples: Input : [True, False, True, True, False] Output : 3 Input : [False, True, False, True] Output : 2 Method #1: Using List comprehension One simple method to count True booleans in a list i
3 min read
Python - Count elements in tuple list Sometimes, while working with data in form of records, we can have a problem in which we need to find the count of all the records received. This is a very common application that can occur in Data Science domain. Letâs discuss certain ways in which this task can be performed. Method #1: Using len()
5 min read