In the above example, we have used dictionary comprehension to create a dictionary namedvowels. Here, the value is not assigned to the keys of the dictionary. But, for each key inkeys, a new list ofvalueis created. The newly created list is assigned each key in the dictionary. Also Read...
Learn how to use the fromkeys method in Python dictionaries to create a new dictionary with specified keys and values effectively.
That is exactly analogous to what you are doing in your example. Member birkenfeld commented Aug 24, 2009 I'll add a bit of explanation as well: I have to add this ugly loop : for key in keys: d[key] = [] With a defaultdict, you don't -- you just use d[key], and if not...