# File: tuThDicts.py # Author: Dr. Gibson # Date: 11/8/2016 # Section: N/A # E-mail: k.gibson@umbc.edu # Description: # This file contains python code that creates a dictionary # from two lists, and performs some other operations as well. def main(): names = ["Tina", "Pratik", "Amber"] majors = ["Social Work", "Pre-Med", "Art"] # create empty dictionary myMajor = {} # iterate over the indexes, not the contents for i in range(len(names)): # adding to a dictionary syntax: # myMajor["key"] = "value myMajor[ names[i] ] = majors[i] print("The created dictionary:") print("\t", myMajor) print() # use the items() method to access each key:value pair as a tuple print("The items in the dictionary as key:value pairs:") print("\t", list(myMajor.items()) ) print() for item in list( myMajor.items() ): print("The item tuple:", item) # split the tuple into its two parts nam, maj = item print("The major chosen by", nam, "is", maj) # check student's majors print("\nUsing the optional argument in get() to set default as 99") print("Amber's major:", myMajor.get("Amber", 99) ) print("Fitia's major:", myMajor.get("Fitia", 99) ) main()