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
|
1116 Discussions
|
Please Login in order to post a comment
from collections import defaultdict
A=defaultdict(list) n,m=list(map(int, input().split())) for i in range(1,n+m+1): word=input() if i<=n: A[word].append(i) elif word in A.keys(): print(*A[word]) else: print(-1)
I really don't see the point of this thing. In most cases I though about I had to check if it was empty at some point anyway or if it was present inside the list so it's just adding overhead and you still need to check. Isn't it useless in most cases ?
Plus in normal dict you can already use: d = dict() d.get(key, default_value) `