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
|
1055 Discussions
|
Please Login in order to post a comment
from collections import defaultdict d = defaultdict(list) list1=[]
n, m = map(int,input().split())
for i in range(0,n): d[input()].append(i+1)
for i in range(0,m): list1=list1+[input()]
for i in list1: if i in d: print(" ".join( map(str,d[i]) )) else: print(-1)
I fail to see the utility of defaultdicts for this problem. List comprehensions provide, IMO, more readable code:
n,m=[int(i) for i in input().split()] l1=[] l2=[] i=0 while(i
The defaultdict in Python is a powerful tool that simplifies dictionary handling by automatically assigning a default value to keys that don’t yet exist. This feature saves time and reduces code complexity, especially when dealing with cases where keys might not be initialized. Bc game