from collections import defaultdict # Creating a dictionary car = { "brand": "Ford", "model": "Mustang", "year": 1964 } print(car["brand"]) print(car["model"]) ''' car["yue"] Traceback (most recent call last): File "", line 1, in car["yue"] KeyError: 'yue' ''' from collections import defaultdict # Missing keys default to 0 counts = defaultdict(int) words = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple'] for word in words: counts[word] += 1 print(counts['apple']) # Output: 3 print(counts['grapes']) # Output: 0 (No KeyError!) counts_dict = {} # modify the for loop to count using a regular dict, counts_dict. for word in words: # counts_dict[word] = counts_dict[word] + 1 if word in counts_dict: counts_dict[word] += 1 else: counts_dict[word] = 1 print(f"counts_dict: {counts_dict}") anagram_dict['acer']: ['acre', 'care', 'race']