Constructing a dictionary in Python is a simple matter of assigning a group of values with associated keys to a variable. Although the values can be any Python object, the keys must either be a number, string, or immutable tuple. Simple dictionaries are made up of simple one-to-one, key-to-value relationships. However, you can construct very complex dictionaries that can have one-to-many and even many-to-many value relationships. A one-to-many relationship can be accomplished by simply using list objects as the values in the dictionary. The many-to-many relationship will take more thought and effort; however, this relationship can be accomplished by using tuples as the key objects and list objects as the value objects in the dictionary. #Simple one to one dictionary numberDict = {1:'one', 2:'two', 3:'three', 4:'four'} #One to many dictionary letterDict = {'vowel':['a','e','i','o','u'],\ 'consonant':['b','c','d','f']} #Many to many dictionary numbers = (1,2,3,4,5,6,7,8,9,0) letters = ('a','b','c','d','e','f') punct = ('.', '!', '?') charSetDict = {numbers:[], letters:[], punct:[]} def_dict.py |