# How to count unique values inside a list

There are a few techniques for finding or counting remarkable things inside a rundown in Python. Here we’ll examine 3 strategies. The quantity of novel qualities in a rundown is the number of components barring copies. For instance, the quantity of novel qualities in [1, 1, 2, 2, 3] is 3.

In this article, we will figure out how to include special qualities present in a rundown in Python. We will utilize some implicit capacities, a basic methodology, and some custom codes also. We should initially have a brief glance over what is a rundown in Python and about exceptional qualities in a rundown in this article.

Consideration nerd! Fortify your establishments with the Python Programming Foundation Course and gain proficiency with the rudiments.

In any case, your meeting arrangements Enhance your Data Structures ideas with the Python DS Course. What’s more in the first place of your Machine Learning Journey, join the Machine Learning – Basic Level Course.

## Python List

Python has an inherent information type called list. It resembles an assortment of clusters with various philosophies. The information inside the rundown can be of any kind say, whole number, string or float esteem, or even a rundown type. The rundown utilizes comma-isolated qualities inside square sections to store information. Records can be characterized utilizing any factor name and afterward relegating various qualities to the rundown in a square section. The rundown is requested, alterable, and permits copy esteems.

list1 = [‘Ram’,’Arun’,‘Kiran’]
list2 = [16,78,32,67]
list3 = [‘apple’,’mango’,16,’cherry’,3.4]

A program will ask the client for information and stores the qualities in an exhibit/list. Then, at that point, a clear line is entered, it will let the client know the number of the qualities are exceptional. The program might have copy components also. At the point when we count the length of the rundown we get the absolute length including the copy components. Yet, in this article, we will perceive how to get the absolute count of the unmistakable components or one of a kind components in a rundown.

In Python, we have copy components present in the rundown. In some cases, we happen in a circumstance where we really want to include extraordinary qualities in the rundown in Python. So we will talk about the different ways of tracking down the novel qualities in an exhibit or rundown. And furthermore, print the quantity of one of a kind components present in the rundown.

One of a kind Values in List are the assortment of unmistakable qualities which are not the equivalent. Ordinarily, we need to bring the nonrepititve values from a rundown. To do as such, you can utilize circles, sets, counter module,s and numerous alternate ways. In this post, we’ll go through each conceivable method for getting extraordinary qualities from the rundown.

### Python Unique component in the rundown

One of a kind components are the components which shows up just one time in a rundown.

Assume, we have a rundown = [1, 2, 3, 2, 3, 5, 1, 6, 1]. Here we see that 1 comes multiple times, 2 comes twice, 3 comes twice, 5 and 6 comes single time. Assuming we include the novel components in the rundown, it will be just 5 as [1, 2, 3, 5, 6].

### For what reason do we have to get the extraordinary components from the rundown?

In numerous situations, the information is gathered so that it adds dull client demands. For instance in IP lumberjack applications, each time a client interfaces with the lumberjack, its IP is attached to the information base. Presently, thinking about this, assuming you need to separate the rundown of IPs associated with the lumberjack, you just need one of a kind qualities.

### Track down Unique Elements

The quantity of exceptional qualities in a rundown is the quantity of components barring copies. For instance, the quantity of exceptional qualities in [1, 1, 2, 2, 3] is 3.

Allow us to take a gander at the beneath ways and acquaint various techniques with count one of a kind qualities inside a rundown. This article utilizes the accompanying strategies:

• Beast Force Approach
• Utilizing Collections module
• Utilizing set() work
• Utilizing NumPy work

### Model: Brute Force Approach to Find Unique Elements

The principal technique is the Brute Force Approach. This method isn’t useful as it requires some venture and space. The beast power approach utilizes a straightforward calculation to count the exceptional qualities and it doesn’t need any capacity to work out remarkable qualities. This technique takes an unfilled rundown and a count variable to count the novel qualities. We explore from the start and actually take a look at every component. Assuming that component is absent in the unfilled rundown, then, at that point, we add that component to the rundown and augmentation the counter by 1. While crossing, in case that component is available in the vacant rundown, we won’t expand the counter factor and will move to the following cycle.

Allow us to comprehend this with the assistance of a model given beneath.

#input list
input_list = [1, 2, 2, 5, 8, 4, 4, 8]

#empty list to store unique values
empty_list = []

#counter variable to count unique values
count = 0

# travesing the array
for ele in input_list:
if(ele not in empty_list):
count += 1
empty_list.append(ele)

#output
print("Count of unique values are:", count)

Count of unique values are: 5

### Model: Using Collections module to Find Unique Elements

This strategy will utilize collections.counter() capacity to include the extraordinary qualities in the given Python list. In this strategy, a capacity named counter() is imported from the assortments module.

assortments It is a Python standard library, and it contains the counter class to count the hashable things.

counter-Counter capacity is utilized to make a word reference Counter is a dict subclass for identifying hashable things. Counter is an unordered assortment where esteems are put away as word reference keys and the consider values are put away word reference esteems. It has two strategies :

• keys() – returns the special qualities in the rundown.
• values() – returns the include of each special worth in the rundown.

In the wake of bringing in the counter capacity from assortments, we announce an info list. From this information list, we make one more rundown comprised of just the things whose key qualities are available once. This rundown is a particular rundown of things. We know counter prints information as a word reference. Thus, the keys of the word reference will be the special things (use counter.keys() work) and the qualities will be the quantity of that vital present in the rundown (use counter.values() work). Therefore, we track down the length of this new rundown. We utilize the len() capacity to get the quantity of novel qualities by getting through the counter class as the contention.

Allow us to comprehend this with the assistance of a model given beneath.

#import Counter
from collections import Counter

#input list
input_list = ['X', 'B', 'G', 'X','G']

#keys of the dictionary will be the unique items
print(Counter(input_list).keys())

#values will be the number of that key present in the list
print(Counter(input_list).values())

#new list with key-value pairs
print(Counter(input_list))

#count of unique values
print("Count- ", len(Counter(input_list)))

dict_keys([‘X’, ‘B’, ‘G’]) dict_values([2, 1, 2]) Counter({‘X’: 2, ‘B’: 1, ‘G’: 2}) Count- 3

1- The main strategy is the savage power approach. This strategy isn’t especially effective as it requires some investment and space. In this strategy, we take an unfilled exhibit and a count variable(set to be zero). We navigate from the beginning and actually look at things. Assuming the thing isn’t in the void list(as it has taken void) then, at that point, we will add it to the vacant rundown and increment the counter by 1. While voyaging assuming the thing is in the taken list(empty list) we won’t count it.

Example:

 # taking an input list input_list = [1, 2, 2, 5, 8, 4, 4, 8]   # taking an input list l1 = []   # taking an counter count = 0   # travesing 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

2- In this strategy, we will utilize a capacity name Counter. The module assortments have this capacity. Utilizing the Counter capacity we will make a word reference. The keys of the word reference will be the one of a kind things and the qualities will be the quantity of that critical present in the rundown. We will make a rundown utilizing the keys, the length of the rundown will be our reply.

 # 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.

3- 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.

 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

Read More: How to take integer input in Python?