Q:

How to count frequency of pairs in a list?

Right now I am able to count the frequency of each word in a list.
    >>> list =['a', 'b', 'a', 'c', 'a', 'c']

frequency = {}
for w in words:
    frequency[w] = frequency.get(w, 0) + 1
return frequency

It gives me this output:

{'a': 3, 'b': 1, 'c: 2'}

But what I'd like for it to give me is the frequency of pairs for each list item.  For example, 'b' comes after 'a' 1 time and 'c' comes after 'a' 2 times.  

{'a':{'b':1,'c':2},'b':{'a':1},'c':{'a':1}}

How would I go about accomplishing this?

A:

If you're willing to accept a slightly different format, it's easy to get the pairwise counts using collections.Counter and zip:
>>> seq = list("abacac")
>>> from collections import Counter
>>> c = Counter(zip(seq, seq[1:]))
>>> c
Counter({('a', 'c'): 2, ('b', 'a'): 1, ('c', 'a'): 1, ('a', 'b'): 1})

If you really want the format you gave, you have a few options, but one way would be to use itertools.groupby to collect all the pairs starting with the same element together:
>>> from itertools import groupby
>>> grouped = groupby(sorted(zip(seq, seq[1:])), lambda x: x[0])
>>> {k: dict(Counter(x[1] for x in g)) for k,g in grouped}
{'a': {'c': 2, 'b': 1}, 'c': {'a': 1}, 'b': {'a': 1}}

