Comparison table of common methods
Method or operation | Function description |
---|---|
dicts .keys() | Returns all key information |
dicts . values() | Returns all value information |
dicts . items() | Returns all key value pairs |
dicts . get (key , defualt ) | If the key exists, the corresponding value is returned; otherwise, the default value is returned |
dicts . pop (key , default ) | If the key exists, the corresponding value is returned, and the key value pair is deleted. Otherwise, the default value is returned |
dicts . popitem() | Randomly take a key value pair from the dictionary and return it in the form of tuple (key, value) |
dicts . clear () | Delete all key value pairs |
del dicts [ key ] | Delete a key value pair from the dictionary |
key in dicts | Returns True if the key is in the dictionary, False otherwise |
dicts . copy() | Copy dictionary |
dicts . update (dicts2) | Update the dictionary. The parameter dict2 is the updated dictionary |
1. keys (), values () and items () methods
- The keys (), values() and items () methods can return the key view, value view and key value pair view of the dictionary respectively.
- The view object is different from the list. It does not support indexing, but can be accessed iteratively. The information of the dictionary can be obtained by traversing the view.
Example 5-10 common dictionary methods keys(), values() and items()
>>> dicts = {"id":101,"name":"Rose","address":"Changjianroad","pcode":"116022"} #Get view of key >>> key1 = dicts.keys() >>> type(key1) <class 'dict_keys'> >>> key1 = dicts.keys() >>> for k in key1: print(k,end=",") id,name,address,pcode, #View to get values >>> values1 = dicts.values() >>> type(values1) <class 'dict_values'> >>> for v in values1: print(v,end=",") 101,Rose,Changjianroad,116022, #Get view of key value pair >>> items = dicts.items() >>> type(items) <class 'dict_items'> >>> for item in items: print(item,end=",") ('id', 101),('name', 'Rose'),('address', 'Changjianroad'),('pcode', '116022'),
2. get(), pop(), popitem() methods
- You can return the value corresponding to the key through the get() method. If the key does not exist, a null value is returned. The default parameter specifies the return value when the key does not exist.
- The pop() method removes the key from the dictionary and returns the corresponding value. If the key does not exist, the default value is returned. If the default parameter is not specified, an exception will be generated when the code runs.
- The popitem () method removes and returns key value pairs from the dictionary. When the dictionary is empty, a keyerror exception will be generated.
Example 5-11 common methods of Dictionary: get(), pop(), popitem()
dicts = {"id":101,"name":"Rose","address":"Changjianroad"} #get() method >>> dicts.get("address") 'Changjianroad' >>> dicts.get("pcode") >>> dicts.get("pcode","116000") #pcode does not exist in the dictionary. The default value is returned '116000' >>> dicts {'id': 101, 'name': 'Rose', 'address': 'Changjianroad'} #pop method >>> dicts.pop('name') 'Rose' >>> dicts {'id': 101, 'address': 'Changjianroad'} >>> dicts.pop("email","ul@u2") #Does not exist in the dictionary, return the default value 'ul@u2' >>> dicts {'id': 101, 'address': 'Changjianroad'} >>> dicts = {"id":101,"name":"Rose","address":"Changjianroad"} #Use the popitem() function to delete key value pairs one by one >>> dicts.popitem() ('address', 'Changjianroad') >>> dicts.popitem() ('name', 'Rose') >>> dicts.popitem() ('id', 101) >>> dicts {}
3. copy() and update () methods
- A copy of a dictionary can be returned through the copy() method, but the id of the new dictionary is different from that of the original dictionary. When a user modifies one dictionary object, it will not affect another dictionary object.
- Through the update() method, you can use one dictionary to update another dictionary. If the two dictionaries have the same key, the key value pair will be overwritten.
Example 5-12 application examples of common dictionary methods copy() and update()
>>> dicts = {"id":101,"name":"Rose","address":"Changjianroad"} #copy() method >>> dict2 = dict1.copy() >>> id(dict1),id(dict2) (2244937983320, 2244936532544) >>> dict1 is dict2 False >>> dict2["id"] = 102 >>> dict2 {'id': 102, 'name': 'Rose', 'address': 'Changjianroad'} >>> dict1 {'id': 101, 'name': 'Rose', 'address': 'Changjianroad'} #update() method >>> dict3 = {"name":"John","email":"u1@u2"} >>> dict1.update(dict3) >>> dict1 {'id': 101, 'name': 'John', 'address': 'Changjianroad', 'email': 'u1@u2'}