DefaultDict Tutorial

  • + 0 comments
    from collections import defaultdict
    
    d = defaultdict(str)
    n,m = map(int,input().split())
    for i in range(n): d[input()]+=f'{i+1} '
    for i in range(m): print(d.get(input(),-1))
    

    .get() allows a simple way to print the value or -1 and avoids creating new values in defaultdict