We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Python
- Collections
- DefaultDict Tutorial
- Discussions
DefaultDict Tutorial
DefaultDict Tutorial
Sort by
recency
|
1132 Discussions
|
Please Login in order to post a comment
I've left my solution below in case it helps anyone. I'm not sure if its the best solution, but it worked!
from collections import defaultdict
if name == 'main': n, m = list(map(int, input().split())) d = defaultdict(list)
Check this: from collections import defaultdict d = defaultdict(list)
n, m = map(int,input().split())
for i in range(1,n+1): d[input()].append(i)
for i in range(m): word_B = input() if word_B in d: print(*d[word_B]) else: print(-1)
I defaulted to not using default dict
I tried to use defaultdict.
Following is my code:
i found myself solving this without the need to use defaultdict (but i feel like it's not the most efficient solution):