Python: Count items using itertools

An approach to counting items from a list of unknown items.

Python is a "batteries included" language, meaning it comes with a large library of useful modules. One such library is "itertools" which offer multiple functions with various use cases. In this post, we will look into one of its functions: unique_everseen

unique_everseen() is part of more_itertools library. The library can be installed via:

 pip install more-itertools

unique_everseen(iterable), takes an iterable parameter and yield unique elements, preserving order.

 >>> list(unique_everseen('AAAABBBCCDAABBB'))
 ['A', 'B', 'C', 'D']

One of the practical usages can be to perform activities like stock count or to prep data for reporting purposes.

In the below example, we have a list of products, assume we have no knowledge of the type or count of items in the list.

 from more_itertools import unique_everseen

 data = ['Product A', 'Product B', 'Product A', 'Product E', 'Product B', 'Product F', 'Product N', 'Product B', 
 'Product N', 'Product A', 'Product B', 'Product A']

 final_count = {k:data.count(k) for k in unique_everseen(data)}
 print(f'We have {len(final_count)} products. The count of each product is as below:\n{final_count}')

The above code will print, the product name as dict key and its count as value:

 We have 5 products. The count of each product is as below:
 {'Product A': 4, 'Product B': 4, 'Product E': 1, 'Product F': 1, 'Product N': 2}

This approach seems simpler and faster than using loops. Remember that list objects are unhashable - you can use the key parameter to transform the list to a tuple (which is hashable) to further improve the performance.

 final_count = {k:data.count(k) for k in unique_everseen(data, key=tuple)}

I hope you've found this article useful. Follow me if you are interested in:

  1. Python

  2. AWS Architecture & Security.

  3. AWS Serverless solutions.