We’ll create a function that executes each method and decorate it to identify how long its execution takes.įor out sample list, we’ll use the first few paragraphs of A Christmas Carol, where each word is a list, and multiply that list by 10,000 to make it a bit of a challenge: import time What we’ll do is create a Python decorator to time each method. Now that you’ve learned four unique ways of counting unique values in a Python list, let’s take a look at which method is fastest.
Let’s see how this can be done: # Use numpy in Python to count unique values in a list We can also write this out in a much faster way, using method chaining.
Use Numpy to Count Unique Values in a Python List Want to learn more? Learn four different ways to append to a list in Python using this extensive tutorial here. This returns the same value but is a little faster to write out. We can also make this process a little faster by simply chaining our methods together, as demonstrated below: # Using sets to count unique values in a list Returned the number of values by counting the length of the set, using the len() function.Turned our list into a set using the built-in set() function.Now, let’s see how we can use sets to count unique values in a list: # Using sets to count unique values in a list When we pass a list into the function, it turns the list into a set, thereby stripping out duplicate values. Python comes built with a set() function that lets you create a set based on something being passed into the function as a parameter. One of the things that separate sets from lists is that they can only contain unique values. This process returns the same thing, but is much quicker to write! Using Sets to Count Unique Values in a Python ListĪnother built-in data structure from Python are sets. # Using Counter from collections to count unique values in a list We can make this much easier to write by simply chaining the process together, as shown below. Finally, we get the length of that new object.We passed our list into the Counter object to create a unique object.
Let’s see how we can use the Counter object to count unique values in a Python list: # Use Counter from collections to count unique values in a Python list
UNIQUE WORD COUNTER HOW TO
Tip! Want to learn more about the Python collections module and its Counter class? Check out my in-depth tutorial here, where you’ll learn how to count occurrences of a substring in a string. The module has a built-in object called Counter that returns a dictionary-like object with the unique values as keys and the number of occurrences for values.īecause of this, we can counts the number of keys to count the number of unique values. The built-in collections module can be used to count unique values in a list. Using Collections to Count Unique Values in a List For example, if you receive data in a list that tracks the number of log in into a site, you could determine how many unique people actually logged in. There may be many times when you may to count unique values contained within a list. Python lists are a useful built-in data structure! One of the perks that they offer is the ability to have duplicate items within them. What Method is Fastest to Count Unique Values in a Python List?.Use a For Loop in Python to Count Unique Values in a List.Use Numpy to Count Unique Values in a Python List.Using Sets to Count Unique Values in a Python List.Using Collections to Count Unique Values in a List.