File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -28,15 +28,28 @@ def main():
2828 print (f"error: { e } " )
2929
3030 # look up: method 2
31- if "no_key " in name_dept : # inefficient because it queries the dict twice
31+ if "key " in name_dept : # inefficient because it queries the dict twice
3232 dept = name_dept ["no_key" ]
3333 print (f"{ dept } " )
3434
3535 # look up: method 3 (recommended)
3636 dept = name_dept .get ("alice" , "no_key" ) # provides the support for default value
3737 print (f"{ dept } " )
3838
39- # add items
39+ # get all keys
40+ print (f"{ name_dept .keys ()} " ) # retuens a object
41+ # get all values
42+ print (f"{ name_dept .values ()} " ) # returns a object
43+ # get key and values in tuple format
44+ print (f"{ name_dept .items ()} " ) # returns a list, each entry is tuple
45+
46+ # print the dict: method 1
47+ for key , value in name_dept .items ():
48+ print (f"{ key } : { value } " )
49+
50+ # print the dict: method 2
51+ for key , value in name_dept :
52+ print (f"{ key } : { name_dept [key ]} " )
4053
4154
4255if __name__ == '__main__' :
You can’t perform that action at this time.
0 commit comments