| by Arround The Web | No comments

Python Count Unique Values in the List

Sometimes we need to count how many items in a Python list appear only once. These are called “Unique Items”. They can be useful when we want to filter out repeated values. There are different ways to find and count unique items in a list, such as the “for” loop, “set()” method, or collections “Counter()” function, etc.

This tutorial will discuss various examples of counting unique elements in the list with different techniques.

How to Count/Determine Unique Values in the List in Python?

The following methods are used to count unique values in the list:

Method 1: Count/Find Unique Values in a List by Utilizing the “set()” Method

The “set()” method is used to create/make a set object. It is an unordered collection of distinct items/elements. This method can be used to count unique element values in the list.

Syntax

set(iterable)

 

In the above syntax, “iterable” is any iterable object, such as a list, tuple, or string.

Example

The below code is used to count unique list values:

names = ['Joseph', 'Anna', 'Lily', 'Henry', 'Joseph']
print(len(set(names)))

 

In the above code, the “set()” method creates a set object by accepting the “list” iterable as an argument. The “len()” function is used to count/determine the unique values returned by the “set()” method.

Output

The unique elements from the given list have been displayed in the above snippet, thereby excluding “Joseph” from the list.

Method 2: Count/Find Unique Values in a List by Utilizing the “collections” Module

The “collections” module provides a “Counter()” function that is used along with the “keys()” function to count/determine the specific unique values in the list.

Example

In the below code, the “Counter()” function of the “collections” module is used to determine how many unique values are in the list:

from collections import Counter
list_value = ['Joseph', 'Anna', 'Lily', 'Henry', 'Joseph']
counter_object = Counter(list_value)
keys = counter_object.keys()
print(len(keys))

 

In the above code lines:

  • The “Counter()” function takes the defined “list” as an argument and creates a dictionary Counter object.
  • The “keys()” function gets the keys of the counter object, which are the unique names in the list, and assigns them to the variable “keys”.
  • A view object is returned that reflects the changes in the dictionary as an iterable.
  • At last, the “len()” function determines the length of the keys, which is the number of unique names in the list.

Output

The above snippet displays the special/unique values in the input list.

Method 3: Count/Find Unique Values in a List by Utilizing a “for” Loop

The “for” loop can also be utilized to count the unique values in the input Python list.

Example

The below code is used to count the unique value in a Python list:

names = ['Joseph', 'Anna', 'Lily', 'Henry', 'Joseph']
no_duplicates = list()
count = 0
for name in names:
    if name not in no_duplicates:
        no_duplicates.append(name)
        count += 1
print(count)

 

In the above code block:

  • The list of names with some duplicates and an empty list named “no_duplicates” is created.
  • The variable named “count” is also created and assigned the value “0”.
  • The “for” loop is used to loop through each name in the “names” list and check if it is already in the empty list.
  • If the name is not in the “no_duplicates” list, it adds it to the list and increments the count by one, thereby omitting the duplication.

Output

Method 4: Count Unique Values in a Python List Using “numpy” Library

The Python “numpy” library functions “numpy.array()” and “numpy.unique()” are used jointly to count the unique values in a Python list.

Example

The following code is used to count the specific/unique values in a list:

import numpy
names = ['Joseph', 'Anna', 'Lily', 'Henry', 'Joseph']
array = numpy.array(names)
unique = numpy.unique(array)
print(len(unique))

 

In the above code:

  • The “array()” function is used to create an array by taking the list as an argument.
  • Similarly, the “unique()” function creates the array by removing the duplicate values.
  • The “len()” function determines/calculates the length of the unique value.

Output

This snippet displays the unique values in a list appropriately.

Conclusion

To count unique values in the Python list, the “set()” function, the “collections” module, the “for” loop, or the “numpy” library are used in Python. A set object is an unordered collection of unique elements that can be created with the “set()” method and used to count distinct values in a list. Similarly, the “Counter()” function of the “collections” module, the “for” loop, or the “numpy” libraries can also be used to count the unique values in the list.

Share Button

Source: linuxhint.com

Leave a Reply