# Python: Count items using itertools

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:

```plaintext
 pip install more-itertools
```

unique\_everseen(iterable), takes an [iterable](https://www.w3schools.com/python/python_iterators.asp) parameter and yield unique elements, preserving order.

```plaintext
 >>> 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.

```plaintext
 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:

```plaintext
 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.

```plaintext
 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.
