Question: I have a dictionary like this 

```
d = {('tom', 'sam'): 1, ('sam', 'i'): 1, ('i', 'am'): 2, 
     ('am', 'i'): 1, ('am', 'sam'): 1}

```

I would like to perform a distinct count in this dictionary, I will expect the answer to be 4 because I will be counting `tom`, `sam`, `i` and `am`.

Answer: 
Your question title is slightly misleading. You are actually looking for distinct items in tuples, that happen to be the keys of a dictionary. You can use the following method:

```
>>> d = {('tom', 'sam'): 1, ('sam', 'i'): 1, ('i', 'am'): 2, 
         ('am', 'i'): 1, ('am', 'sam'): 1}
>>> len(set(item for key in d for item in key))
4

```
Using [`itertools.chain`](https://docs.python.org/3/library/itertools.html#itertools.chain), you can get rather concise:

```
from itertools import chain

len(set(chain(*d)))
# 4

```

This uses the fact that iterating a `dict` iterates its keys which in your case, are iterables themselves. Hence, unpacking (`*`) and chaining the `dict` produces a flat iterator over the elements in the key `tuples`.