| Serial number |
Functions and descriptions |
Instance |
| 1 |
Radiansdict. clear ()
Delete all elements in the dictionary |
>>> Dict = {'name': 'cnblogs', 'age': 7, 'class': 'first '}
>>> Dict. clear ()
>>> Dict
{} |
| 2 |
Radiansdict. copy ()
Returns the shortest copy of a dictionary. |
>>> Dict = {'name': 'cnblogs', 'age': 7, 'class': 'first '}
>>> D = dict. copy ()
>>> D
{'Name': 'cnblogs', 'age': 7, 'class': 'First '} |
| 3 |
Radiansdict. fromkeys (seq [, value])
Create a new dictionary and use the elements in sequence seq as the dictionary key. val is the initial value corresponding to all the keys in the dictionary. |
Seq = ('name', 'age', 'sex ')
Dict = dict. fromkeys (seq) Print ("new dictionary: % s" % str (dict ))
Dict = dict. fromkeys (seq, 10) Print ("new dictionary: % s" % str (dict ))
======= Output: ====== The new dictionary is {'name': None, 'age': None, 'sex': None} The new dictionary is {'name': 10, 'age': 10, 'sex': 10} |
| 4 |
Radiansdict. get (key, default = None)
Returns the value of the specified key. If the value is not in the dictionary, the default value is returned. |
Dict = {'name': 'roy, 'age': 100}
Print ("Age value: % s" % dict. get ('age '))
Print ("Sex value: % s" % dict. get ('sex', "nan "))
======= Output: ====== Age: 100 Sex value: nan
|
| 5 |
Key in dict
If the key returns true in the dictionary dict, otherwise false is returned. |
Dict = {'name': 'roy, 'age': 100}
# Check whether the key Age exists
If 'age' in dict:
Print ("key Age exists ")
Else:
Print ("key Age does not exist ")
# Check whether the Sex key exists
If 'sex' in dict:
Print ("key Sex exists ")
Else:
Print ("key Sex does not exist ")
======= Output: ======
Key Age exists
Key Sex does not exist |
| 6 |
Radiansdict. items ()
Returns a list of traversal (Key, value) tuples. |
Dict = {'name': 'roy, 'age': 100}
Print ("Value: % s" % dict. items ())
======= Output: ======
Value: dict_items ([('name', 'roy '), ('age', 100)]) |
| 7 |
Radiansdict. keys ()
Returns all keys of a dictionary in a list. |
Dict = {'name': 'roy, 'age': 100}
Print ("all dictionary keys: % s" % dict. keys ())
======= Output: ======
All keys in the dictionary are dict_keys (['name', 'age']). |
| 8 |
Radiansdict. setdefault (key, default = None)
Similar to get (), but if the key does not exist in the dictionary, a key is added and the value is set to default. |
Dict = {'name': 'roy, 'age': 100}
Print ("Age key value: % s" % dict. setdefault ('age', None ))
Print ("Sex key value: % s" % dict. setdefault ('sex', None ))
Print ("new dictionary:", dict)
======= Output: ======
Age key value: 100
Sex key value: None
New Dictionary: {'name': 'roy ', 'age': 100, 'sex': None} |
| 9 |
Radiansdict. update (dict2)
Update the dictionary dict2 key/value pairs to dict. |
Dict = {'name': 'roy, 'age': 100}
Dict2 = {'sex ': 'female '}
Dict. update (dict2)
Print ("Update dictionary dict:", dict)
======= Output: ======
Update dictionary dict: {'name': 'roy ', 'age': 100, 'sex': 'female '} |
| 10 |
Radiansdict. values ()
Returns all values in the dictionary as a list. |
Dict = {'name': 'roy, 'age': 100}
Print ("all dictionary values:", list (dict. values ()))
======= Output: ======
All dictionary values: ['roy ', 100] |
| 11 |
Pop (key [, default])
Delete the value corresponding to the specified key in the dictionary. The returned value is the deleted Value. The key value must be given. Otherwise, the default value is returned. |
Dict = {'name': 'roy, 'age': 100}
Pop_obj = dict. pop ('name ')
Print (pop_obj)
======= Output: ======
Roy |
| 12 |
Popitem ()
Returns and deletes a pair of keys and values in the dictionary randomly (generally, the end pair is deleted ). |
Dict = {'name': 'roy ', 'age': 100, 'sex': 'nan '}
Pop_obj = dict. popitem ()
Print (pop_obj)
Print (dict)
======= Output: ======
('Sex', 'nan ')
{'Name': 'roy ', 'age': 100} |