How to initialize defaultdict with keys?

From the comments, I’m assuming you want a dictionary that fits the following conditions:

  1. Is initialized with set of keys with an empty list value for each
  2. Has defaultdict behavior that can initialize an empty list for non-existing keys

@Aaron_lab has the right method, but there’s a slightly cleaner way:

d = defaultdict(list,{ k:[] for k in ('a','b','c') })

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)