Python Tuple Frequency
tuples + CounterPractice

Count neighboring word pairs as tuple keys

Each adjacent pair becomes a tuple like ('to','be'). Tuples can be dictionary keys because they are immutable.

Picture this: tuple as a dict key

Because tuples are immutable, they can be dictionary keys. Lists cannot.

(name, year)key
→
dict[key]store/lookup
→
valuethe payload

In easy words

From words w0 w1 w2 ..., make pairs (w0,w1), (w1,w2), .... Count which pair appears most.

Where this shows up

Autocomplete, spam filters, and language models all look at which words follow which other words.

Pair counter

Words: to be to be

Most common: ('to', 'be') 2

Example Program

1words = input().split()
2from collections import Counter
3pairs = Counter((words[i], words[i+1]) for i in range(len(words)-1))
4key = max(pairs, key=pairs.get)
5print(f"{key} {pairs[key]}")