dict1 = {"asdf":34, "zxcv":456, "qwerty":123, 234:"hello"} print(type(dict1)) print(len(dict1)) print(dict1) print(dict1.items()) #list of tuples(key,value) print(dict1.keys()) print(dict1.values()) print() # initialize with key=value arguments. NB not "", also key cannot be number dict2 = dict(asdf=123, qwerty=55555, zxcv=4321) print("dict2:",dict2) # initialize with list of tuples of key,value dict3 = dict([("asdf",345),("qwerty",99999),("zxcv",123),(234,"hello")]) print("dict3:",dict3) print() for i in dict1.items(): #tuple of (key,value) print(i, end=" ") print() for k,v in dict1.items(): print(k,v, end=" ") print() for k in dict1.keys(): print(k,dict1[k], end=" ") print() for k in dict1: #is the keys print(k,dict1[k], end=" ") print() for v in dict1.values(): print(v, end=" ") print() #for k,v in dict1: # print(k,v, end=" ") #print() if "zxcv" in dict1.keys(): print("zxcv is in dict1.keys()") if 123 in dict1.values(): print("123 is in dict1.values()") print(dict1.get("DFGDFG","Not found")) # dictionary comprehension. "merge" 2 lists into one dict keys = ['color', 'fruit', 'pet'] values = ['blue', 'apple', 'dog'] a_dict = {key: value for key, value in zip(keys, values)} print(a_dict) # switch key and value into new dictionary a_dict = {'one': 1, 'two': 2, 'thee': 3, 'four': 4} new_dict = {} for key, value in a_dict.items(): new_dict[value] = key print(new_dict) #or do the switch this way: a_dict = {'one': 1, 'two': 2, 'thee': 3, 'four': 4} new_dict = {value: key for key,value in a_dict.items()} print(new_dict) # copy d1 = dict1.copy() d2 = dict(dict1)