""" Purpose: introduce the dict type """ # initialize dictionary and print it out flora_to_color = { 'spruce': 'green', 'apple': 'red', 'shamrock': 'green', 'banana': 'yellow', 'potato': 'brown', 'eggplant': 'purple' } print( "flora_to_color: ", flora_to_color ) print(); input( "Enter when ready. " ); print() # determine number of mappings nbr_mappings = len( flora_to_color ) # using the len() function will give you how many mappings there are in the dictionary - keys are unique, values are not # should be the number of colons when you print the dict print( "len( flora_to_color): ", nbr_mappings ) print(); input( "Enter when ready. " ); print() # access individual dictionary entry c1 = flora_to_color[ 'potato' ] # use [] to access value associated with a key in the dictionary # this will not change the dictionary, because the dictionary is on the right of the assignment operator ( = ) # only c1 is changed in this # **this will ONLY work to access the value associated with a key. you can't search by value in this way print( "flora_to_color[ 'potato' ]: )", c1 ) print(); input( "Enter when ready. " ); print() # this is to break up the output so we can talk about what's happening # add mapping to dictionary flora_to_color[ 'lemon' ] = 'yellow' # creates a new mapping with 'lemon' as the key mapping to the value 'yellow' # this works becaue 'lemon' has not been used as a key in the dict # this won't necessarily go to the "end" of the dictionary, because there is no end to it # with lists, we had order because elements were indexed by their position # in a dict, we don't have order because values are mapped to keys - there is no "first" element in a dictionary # data structures can be dynamic, so if you search something often it will keep that information close and easy to find # dict[ key ] = value # if key is already in the dictionary, this will overwrite the mapping and update it # if the key is not already in the dictionary, this will add a new mapping of the key to the value print( "flora_to_color: ", flora_to_color ) print(); input( "Enter when ready. " ); print() # reset dictionary mapping flora_to_color[ 'potato' ] = 'golden' # each key is unique, so because 'potato' was already used as a key, we lose the old mapping to 'brown' and replace # it with a new mapping to 'golden' # dictionaries are like lists that use keys instead of indices to locate values print( "flora_to_color: ", flora_to_color ) # print( "c1:", c1 ) c1 is still 'brown', because that is what 'potato' mapped to when c1 was assigned above print(); input( "Enter when ready. " ); print() # get the keys and values of the dictionary --> dictionary functions! flora_keys = flora_to_color.keys() # this hands back a special sequence ( dict_keys ) containing all of the keys ( unordered! ) color_values = flora_to_color.values() # this hands back a special sequence called dict_values of all of the values # not all built-in functions will work with dict_keys or dict_values because they aren't quite lists print( "flora_to_color.keys(): ", flora_keys ) print( "flora_to_color.values(): ", color_values ) print(); input( "Enter when ready. " ); print() # convert keys and values to lists flora = list( flora_keys ) # we can use the list() function to cast our dict_keys into a list colors = list( color_values ) # use list() to cast dict_values to a list # any built-in function that works on a list will work on our flora and colors lists print( "list( flora_to_color.keys() ): ", flora ) print( "list( flora_to_color.values() ): ", colors ) print(); input( "Enter when ready. " ); print() # loop through the mappings for key in flora_to_color: # key is our loop variable. what would that indicate that we're looping through? # **when you loop through a dict in a for loop, your loop variable will take on each key value** value = flora_to_color[ key ] # find the value using the key print( key, ":", value ) flora_to_color[ 'potato' ] = [ 'red', 'yellow', 'golden', 'brown', 'purple' ] print( "flora_to_color:", flora_to_color ) # notes: # number of keys = number of values = len( dict ) # keys must be unique, values can be repeated # keys must map to one value. that value can be a list if you want to associate multiple values with a single key # in operator: key in dict # returns True if key is present and False if it is not print(); input( "Enter when ready. " ); print() c2 = flora_to_color.get( 'green' ) # val = dict.get( key ) # this will hand back None ( because 'green' is not a key ), which means there is nothing there # .get( key ) will not result in an error like [ key ] if the key is not in the dictionary, so it is considered safer # how might the .get() function work? # try-catch blocks: try it out, look for an error and catch that error and do something else # try [] operator # catch error and hand back nothing if it does not work print("c2:", c2 ) print(); input( "Enter when ready. " ); print() del flora_to_color[ 'shamrock' ] # del d[ key ] will delete the mapping of that key and updates the dictionary print( "flora_to_color:", flora_to_color ) # python variables do not remember their old values, the dictionary never knew it had a 'shamrock' mapping