the built-in collections module has useful classdefaultdictwhich makes this even easier one is created by passing type or function for generating the default value for each slot in the dictfrom collections import defaultdict by_letter defaultdict(listfor word in wordsby_letter[word[ ]append(wordthe initializer to defaultdict only needs to be callable object ( any function)not necessarily type thusif you wanted the default value to be you could pass function returning counts defaultdict(lambda valid dict key types while the values of dict can be any python objectthe keys have to be immutable objects like scalar types (intfloatstringor tuples (all the objects in the tuple need to be immutabletoothe technical term here is hashability you can check whether an object is hashable (can be used as key in dictwith the hash functionin [ ]hash('string'out[ ]- in [ ]hash(( ( ))out[ ] in [ ]hash(( [ ])fails because lists are mutable typeerror traceback (most recent call lastin (---- hash(( [ ])fails because lists are mutable typeerrorunhashable type'listto use list as keyan easy fix is to convert it to tuplein [ ] {in [ ] [tuple([ ]) in [ ] out[ ]{( ) set set is an unordered collection of unique elements you can think of them like dictsbut keys onlyno values set can be created in two waysvia the set function or using set literal with curly bracesin [ ]set([ ]out[ ]set([ ] appendixpython language essentials www it-ebooks info