1import collections
2a = collections.defaultdict(collections.Counter)
3inf = [('fruit','apple'),('car','truck'),('fruit','banana'),('fruit','banana')]
4for category,item in inf:
5 a[category][item] = a[category][item] + 1
6#print(a)
7#{'fruit': Counter({'banana': 2, 'apple': 1}), 'car': Counter({'truck': 1})})
1IDs = ['emp1','emp2','emp3']
2
3EmpInfo = [{'name': 'Bob', 'job': 'Mgr'},
4 {'name': 'Kim', 'job': 'Dev'},
5 {'name': 'Sam', 'job': 'Dev'}]
6
7D = dict(zip(IDs, EmpInfo))
8
9print(D)
10# Prints {'emp1': {'name': 'Bob', 'job': 'Mgr'},
11# 'emp2': {'name': 'Kim', 'job': 'Dev'},
12# 'emp3': {'name': 'Sam', 'job': 'Dev'}}
1people = {1: {'name': 'John', 'age': '27', 'sex': 'Male'},
2 2: {'name': 'Marie', 'age': '22', 'sex': 'Female'}}
3
4people[3] = {}
5
6people[3]['name'] = 'Luna'
7people[3]['age'] = '24'
8people[3]['sex'] = 'Female'
9people[3]['married'] = 'No'
10
11print(people[3])
12
13#Output:
14# {'name': 'Luna', 'age': '24', 'sex': 'Female', 'married': 'No'}
1D = {'emp1': {'name': 'Bob', 'job': 'Mgr'},
2 'emp2': {'name': 'Kim', 'job': 'Dev'},
3 'emp3': {'name': 'Sam', 'job': 'Dev'}}