In this DefaultDict problem, we need to develop a python program that can read two integer inputs separated by space. and then we need to use the default dictionary in python and need to append the values and then we need to print them on the output screen. 


HackerRank DefaultDict Tutorial solution in Python


Problem solution in Python 2 programming.

from collections import defaultdict
d = defaultdict(list)
n,m = map(int, raw_input().split())
for i in range(1,n+1):
    d[raw_input()].append(str(i))

for _ in range(m):
    w = raw_input()
    if w in d:
        print " ".join(d[w])
    else:
        print -1


Problem solution in Python 3 programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
from collections import defaultdict
d = defaultdict(list)
list1=[]
n, m = map(int,input().split())
for i in range(1, n+1):
    d[input()].append(str(i))


for i in range(m):
    b = input()
    if b in d: print(' '.join(d[b]))
    else: print(-1)


Problem solution in pypy programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
from collections import defaultdict
d, n = defaultdict(list), list(map(int, raw_input().split()))

for i in xrange(n[0]):
    d[raw_input()].append(i + 1)

for i in xrange(n[1]):
    print ' '.join(map(str, d[raw_input()])) or -1


Problem solution in pypy3 programming.

from collections import defaultdict
d=defaultdict(list)
n,m=map(int,input().split())
x=[]
for i in range(1,n+m+1):
    c=input()
    if(i>n):
        if(d[c]==[]):
            d[c].append(-1)
            x.append(d[c])
        else:
            x.append(d[c])
    else:
        d[c].append(i)

for i in range(m):
    print(" ".join(list(map(str,(x[i])))))