304 North Cardinal St.
Dorchester Center, MA 02124

# DefaultDict Tutorial Hacker Rank Solution – Queslers

## Problem: DefaultDict Tutorial Hacker Rank Solution

The defaultdict tool is a container in the collections class of Python. It’s similar to the usual dictionary (dict) container, but the only difference is that a defaultdict will have a default value if that key has not been set yet. If you didn’t use a defaultdict you’d have to check to see if that key exists, and if it doesn’t, set it to what you want.

For Example :

```from collections import defaultdict
d = defaultdict(list)
d['python'].append("awesome")
d['something-else'].append("not relevant")
d['python'].append("language")
for i in d.items():
print i
```

This prints :

```('python', ['awesome', 'language'])
('something-else', ['not relevant'])
```

In this challenge, you will be given 2 integers, n and m. There are n words, which might repeat, in word group A. There are m words belonging to word group B. For each m words, check whether the word has appeared in group A or not. Print the indices of each occurrence of m in group A. If it does not appear, print -1

#### Constraints :

• 1 <= n <= 10000
• 1 <= m <= 100
• 1 <= length of each word in the input <= 100

#### Input Format :

The first line contains integers, n and m separated by a space.
The next n lines contains the words belonging to group A

#### Output Format :

Output m lines.
The ith line should contain the 1-indexed positions of the occurrences of the ith word separated by spaces.

#### Sample Input :

``````5 2
a
a
b
a
b
a
b
``````

#### Sample Output :

``````1 2 4
3 5
``````

#### Explanation :

‘a’ appeared 3 times in positions 1, 2 and 4.
‘b’ appeared 2 times in positions 3 and 5.
In the sample problem, if ‘c’ also appeared in word group B, you would print -1.

### DefaultDict Tutorial Hacker Rank solution in python 2

``````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``````

### DefaultDict Tutorial Hacker Rank solution in python 3

``````# 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)``````

### DefaultDict Tutorial Hacker Rank solution in pypy

``````# 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):
d[raw_input()].append(i + 1)

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

### DefaultDict Tutorial Hacker Rank solution in pypy 3

``````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])))))``````
##### DefaultDict Tutorial Hacker Rank Solution Review:

In our experience, we suggest you solve this DefaultDict Tutorial Hacker Rank Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

DefaultDict Tutorial problem is available on Hacker Rank for Free, if you are stuck anywhere between a compilation, just visit Queslers to get DefaultDict Tutorial Hacker Rank Solution

##### Conclusion:

I hope this DefaultDict Tutorial Hacker Rank Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Hacker Rank, Leetcode, Codechef, Codeforce Solution.

This Problem is intended for audiences of all experiences who are interested in learning Programming in a business context; there are no prerequisites.

Keep Learning!

More Hacker Rank Problem & Solutions >>

Staircase Hacker Rank Solution

A Very Big Sum Hacker Rank Solution

Diagonal Difference Hacker Rank Solution

Nested Lists Hacker Rank Solution

Lists Hacker Rank Solution