Q:

How to print out the values with count in dictionary using Counter

My code is below 
def tuptodi(tup, dictio): 
    for a, b in tup: 
        dictio.setdefault(a, []).append(b) 
    return dictio
tups = [('key1', 'value1'), ('key2', 'value1'),('key3', 'value3'),('key1', 'value1'),('key1', 'value2')]
dictionary = {} 
print (tuptodi(tups, dictionary)) 

My Out
{'key1': ['value1', 'value1', 'value2'], 'key2': ['value1'], 'key3': ['value3']}

My desired Out
{'key1':{'value1':2},
{'key2':{'value1':1},
{'key3':{'value3':1}

How to achieve this by from collections import Counter

A:

Using collections. Counter and defaultdict:
from collections import Counter, defaultdict

tups = [('key1', 'value1'), ('key2', 'value1'),('key3', 'value3'),('key1', 'value1'),('key1', 'value2')]
cnt = defaultdict(Counter)

for i,j in tups:
    cnt[i].update([j])

cnt

Output:
defaultdict(collections. Counter,
            {'key1': Counter({'value1': 2, 'value2': 1}),
             'key2': Counter({'value1': 1}),
             'key3': Counter({'value3': 1})})

