Counting number of unique values in a Python list Last Updated : 16 Jan, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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. Python li = [1, 2, 2, 3, 4, 4, 5] cnt = len(set(li)) print(cnt) Output5 Explanation:list li is converted to a set, which removes duplicate values, leaving only unique elements.len() function is used to count the number of unique elements in the set, resulting in the total count of distinct values in the list Using collections.CounterUsing collections.Counter allows us to easily count the frequency of each element in a list or string. It creates a dictionary-like object where the keys are the elements and the values are their respective counts. Python from collections import Counter li = [1, 2, 2, 3, 4, 4, 5] c = Counter(li) cnt = len(c) print(cnt) Output5 Explanation:Counter from the collections module is used to count the occurrences of each element in the list li, resulting in a dictionary-like object c with elements as keys and their frequencies as values.len(c) function counts the number of unique keys in the Counter object, which corresponds to the number of distinct elements in the list li.Using List Comprehension and a Temporary ListUsing list comprehension and a temporary list allows for efficiently filtering or modifying elements in a list based on specific conditions. First, a temporary list stores the filtered elements and then list comprehension processes or creates a new list based on that temporary data. Python a = [1, 2, 2, 3, 4, 4, 5] b = [] [b.append(x) for x in l if x not in a] cnt = len(b) print(cnt) Output5 Explanation:List comprehension iterates over each element in l and appends it to the unique_lst if it is not already present, ensuring only unique elements are added.len(unique_lst) calculates the number of distinct elements in the unique_lst, representing the count of unique values in the original list l. Comment More infoAdvertise with us Next Article Python | Count tuples occurrence in list of tuples Y Yash_R Follow Improve Article Tags : Python Python Programs python-list Python list-programs Practice Tags : pythonpython-list Similar Reads Python | Assign value to unique number in list We can assign all the numbers in a list a unique value that upon repetition it retains that value retained to it. This is a very common problem that is faced in web development when playing with id's. Let's discuss certain ways in which this problem can be solved. Method #1: Using enumerate() + list 7 min read Python | Find number of lists in a tuple Given a tuple of lists, the task is to find number of lists in a tuple. This is a very basic problem but can be useful while making some utility application. Method #1: Using len Python3 # Python code to find number of list in a tuple # Initial list Input1 = ([1, 2, 3, 4], [5, 6, 7, 8]) Input2 = ([1 4 min read Python - Unique values count of each Key Given a Dictionaries list, the task is to write a Python program to count the unique values of each key. Example: Input : test_list = [{"gfg" : 1, "is" : 3, "best": 2}, {"gfg" : 1, "is" : 3, "best" : 6}, {"gfg" : 7, "is" : 3, "best" : 10}] Output : {'gfg': 2, 'is': 1, 'best': 3} Explanation : gfg ha 7 min read Python | Count tuples occurrence in list of tuples Many a time while developing web and desktop products in Python, we use nested lists and have several queries about how to find the count of unique tuples. Let us see how to get the count of unique tuples in the given list of tuples. Below are some ways to achieve the above task. Method #1: Using It 5 min read Python program to unique keys count for Value in Tuple List Given dual tuples, get a count of unique keys for each value present in the tuple. Input : test_list = [(3, 4), (1, 2), (2, 4), (8, 2), (7, 2), (8, 1), (9, 1), (8, 4), (10, 4)] Output : {4: 4, 2: 3, 1: 2} Explanation : 3, 2, 8 and 10 are keys for value 4. Input : test_list = [(3, 4), (1, 2), (8, 1), 6 min read How to Get the Number of Elements in a Python List In Python, lists are one of the most commonly used data structures to store an ordered collection of items.In this article, we'll learn how to find the number of elements in a given list with different methods.ExampleInput: [1, 2, 3.5, geeks, for, geeks, -11]Output: 7Let's explore various ways to do 3 min read Like