Python DefaultDict of collections module. The following code shows how to solve the HackerRank DefaultDict Tutorial Python collections problem.
Code:
from collections import defaultdict
n, m = input().split()
d = defaultdict(list)
for i in range(0, int(n)):
d[input()].append(i+1)
for i in range(0, int(m)):
item = input()
if len(d[item]) == 0:
print(-1)
else:
print(*d[item])
Test Input:
5 2
a
a
b
a
b
a
b
Test Output:
1 2 4
3 5