Count unique elements in list python

How to count unique worths in a Python list? There are multiple ways and in this tutorial we will certainly go via them as a method for you to exercise numerous Python constructs.

You watching: Count unique elements in list python

The easiest way to count unique values in a Python list is to transform the list to a collection given that all the facets of a set are unique. You have the right to additionally count unique values in a list using a dictionary, the collections.Counter course, Numpy.unique() or Pandas.unique().

Many of our examples will certainly occupational on the very same list of numbers:numbers = <2, 3, 3, 6, 8, 8, 23, 24, 24, 30, 45, 50, 51, 51>We will implement eincredibly approach in its very own function so it’s simple to call them from the “main” of our code. Eexceptionally feature takes the numbers list as input and returns the list with unique worths.The main of our regimen will certainly then count the number of distinct values utilizing the list len() method.

Let’s get started!

1. Using a For Loop to Count Unique Values in a Python List

The initially method supplies a Python for loop to go via all the facets.def get_unique_values_with_for_loop(values): unique_values = <> for worth in values: if worth not in unique_values: unique_values.append(value) rerevolve unique_valuesThe for loop goes with eincredibly aspect of the list and adds that aspect to a new list if it’s not already existing in the new list.Let’s create a generic code to print the outcome returned by this function so we can usage pretty much the same code to print the result went back by the other functions we will create in the following sections.

numbers = <2, 3, 3, 6, 8, 8, 23, 24, 24, 30, 45, 50, 51, 51>unique_worths = get_unique_values_with_for_loop(numbers)number_of_unique_values = len(unique_values)print("The list of distinct values is: ".format(unique_values))print("The number of unique worths is: ".format(number_of_unique_values))The output is:The list of unique worths is: <2, 3, 6, 8, 23, 24, 30, 45, 50, 51>The variety of unique values is: 10The only line we will certainly need to update once calling a different attribute is the following:unique_worths = get_unique_list_with_for_loop(numbers)Everything else in the main of our routine will continue to be the same bereason it depends on the variable unique_values.

See more: Customer Reviews: Red Kayak Book Review : Red Kayak: Did It Sink Or Float?

2. Count Unique Values in a Python List Using a Set

The set is a file framework that just contains unique elements. We deserve to use this property of the collection to obtain earlier simply distinctive values.def get_unique_values_with_set(values): rerevolve set(values)...unique_values = get_unique_values_with_set(numbers)...Notice the curly brackets in the output due to the fact that the attribute returns a set:

The list of distinct worths is: 2, 3, 6, 8, 45, 50, 51, 23, 24, 30The variety of distinct values is: 10Do you view that the order of the facets has adjusted compared to the original list?That’s because…The collection data type is unordered.The nice point of this strategy is that the code is a lot even more concise (simply a single line function).

3. Using a Thesaurus to Get the Number of Unique Values in a List

Could we usage the truth that dictionary keys are distinct to gain distinct aspects from the original list?We can develop a dictionary where the distinctive facets in the list are the keys of the dictionary. The values of the dictionary are counters that tell us exactly how many kind of time each facet shows up in the list.def get_unique_values_with_dictionary(values): new_values = for worth in values: if value in new_values.keys(): new_values += 1 else: new_values = 1 print(new_values) rerevolve new_worths.keys()...unique_worths = get_unique_values_with_dictionary(numbers)...At each iteration of the for loop we verify if a specific worth already exists as dictionary vital. If it doesn’t we include that vital to the dictionary and also we set the value to 1.Otherwise we increase the value associated to the existing key in the dictionary.I have actually included a print() statement in the function to present you the dictionary developed.2: 1, 3: 2, 6: 1, 8: 2, 23: 1, 24: 2, 30: 1, 45: 1, 50: 1, 51: 2The list of distinctive worths is: dict_keys(<2, 3, 6, 8, 23, 24, 30, 45, 50, 51>)The variety of unique values is: 10

4. Collections.Counter To Count Unique Values in a List

5. Counting Unique Values in a List Using NumPy

The NumPy library additionally offers a way to count distinctive values in a list (or array).
*

6. Using Pandas to Get Unique Values in a List

We deserve to additionally obtain distinct values in a list making use of Pandas. To do that we will usage pandas.unique.
*
It takes as input a one-dimensional range, so let’s create our function:import pandas as pd...def get_unique_values_with_pandas(values): rerotate pd.unique(pd.Series(values))...unique_worths = get_unique_values_with_pandas(numbers)...The output went back by our regime is:The list of distinct values is: < 2 3 6 8 23 24 30 45 50 51>The number of unique worths is: 10

7. Count Unique Values in a List of Dictionaries

8. Which One is the Faster Approach to Count Unique List Values?

Conclusion

After reading this write-up you have many different methods to count distinctive values in a Python list.I hope you have actually discovered this tutorial valuable to watch just how you can deal with the exact same trouble in many kind of different methods using Python.Which one of the remedies we have actually implemented carry out you prefer?