Skip to content
Programming101
Programmingoneonone
  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

HackerRank DefaultDict Tutorial solution in Python

YASH PAL, 31 July 202413 June 2025

In this HackerRank DefaultDict Tutorial in python problem 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.

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.

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])))))

coding problems solutions Hackerrank Problems Solutions Python Solutions

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes