1. When a dictionary is used, it is more suitable than a list. For example, it represents the status of the game board. Each key is a tuple consisting of coordinate values. It stores the number of file modifications, use the file name as the key; digital phone number/address book. Assume that a name is listed as follows: >>> names = ['Alice ', 'beth', 'cecil ', 'dee ', 'earl'] The list of phone numbers is as follows: >>> numbers = ['000000', '000000', '000000', '000000 ', after creating these lists, you can find the Cecil phone number in the following way: >>> numbers [names. index ('cecil ')] '123456' is not practical. 2. to create and use a dictionary, you can create phonebook = {'Alice ': "2341', 'beth ': '123', 'cecil' in the following method ': '000000'} The dictionary consists of multiple keys and their corresponding values (we call key/value pairs an item). In the preceding example, the name is a key, the telephone number is a value. An empty dictionary consists of two braces, such as: {} 3. dict functions can be used to map data using other dictionaries or (keys and values) create a dictionary for such a sequence pair. >>> Items = [('name', 'gumby'), ('age', 42)] >>> d = dict (items) >>> d {'age ': 42, 'name': 'gumby'} The dict function can also create a dictionary using a keyword parameter, as shown in the following example: >>> d = dict (name = 'gumby ', age = 42) >>> d {'age': 42, 'name': 'gumby'} 4. Basic dictionary operations are similar to sequences in many aspects: len (d) returns the number of items in d; d [k] returns the value associated with the key k; d [k] = v associates the value v with the key k; del d [k] deletes key-k items. k in d checks whether d contains key-k items. 5. format the dictionary string >>> phonebook {'beth ': '000000', 'Alice': '000000', 'ceil ': '000000' >>> "Ceil's phone number is % (Ceil) s. "% phonebook" Ceil's phone number is 3258. "In addition to the added string key, the conversion specifiers still work as before. 6. Dictionary Method (1) Clear all items in the dictionary using the clearclear method. First case: >>>> x ={}>> y = x >>>> x ['key'] = 'value' >>>> y {'key ': 'value' >>>> x ={}>>> y {'key': 'value'} Scenario 2: >>> x = {}>>> y = x >>> x ['key'] = 'value' >>> y {'key ': 'value' }>>> x. in clear () >>> y {}, x and y initially correspond to the same dictionary. in Case 1, by associating x to a new empty dictionary, y has no effect on y at all, and y is also associated with the original dictionary. to clear all elements of the original dictionary, you must use the clear method. (2) The copycopy method returns a new dictionary with the same key-value pairs (this method implements shallow replication, because the values are the same, not copies) >>> x = {'username': 'admin', 'machines ': ['foo', 'bar', 'baz'] >>> y = x. copy () >>> y {'username': 'admin', 'machines ': ['foo', 'bar ', 'baz'] }>>> y ['username'] = 'mlh' >>> y ['machines ']. remove ('bar') >>> y {'username': 'mlh', 'machines ': ['foo ', 'baz'] >>>> x {'username': 'admin', 'machines ': ['foo ', 'baz']} when the value is replaced in the copy, the original dictionary is not affected, but if a value is modified (in situ, instead of replacing ), The original dictionary will also change, because the same value is also stored in the original dictionary. One way to avoid this problem is to use deep copy> from copy import deepcopy> d ={}>> d ['names'] = ['qiqi ', 'qiqijianglu']> c = d. copy () >>> dc = deepcopy (d) >>> d ['names']. append ('cccccc') >>> c {'names': ['qiqi', 'qiqianglu', 'cccccccc'] }>>> dc {'names ': ['qiqi', 'qiqianglu']} (3) the fromkeysfromkeys method creates a new dictionary using the given key. The default value of each key is None >> {}. fromkeys (['name', 'age']) {'age': None, 'name': None }>>> dict. fromkeys (['name', 'age']) {'age': None ,' You can also provide the default value >>> dict. fromkeys (['name', 'age'], 'unknown ') {'age': 'unknown', 'name': 'unknown '} (4) when getget accesses a key that does not exist, it will not get an exception, but will get the value of None> print d. get ('name') None you can also define the default value to replace None (5) has_key: Check whether the dictionary contains the given key >>>> d ={}>> d {}>> d. has_key ('name') False >>> d ['name'] = 'qiqi' >>> d. has_key ('name') True (6) items and iteritemsitems return all dictionary items in list >>>> d = {'qiqi ': 111, 'Alice ': 222 >>>> d. items () [('qiqi ', 111), ('Alice ', 222)] The iteritems method works roughly the same. An iterator object instead of the List is returned >>> it = d. iteritems () >>> it <dictionary-itemiterator object at 0x1fe81b0 >>>> list (it) [('qiqi ', 111), ('Alice', 222)] (7) the keys and iterkeyskeys Methods return keys in the dictionary in the form of a list, while the iterkeys return the iterator (8) poppop method for the key to obtain the value corresponding to the given key, then remove this key-value pair from the dictionary (9) the last element in the popitem pop-up list. The popitem pop-up is a random item, because the dictionary does not have the concept of order (10) the setdefasetsetdefault method is similar to get, that is, it can obtain the value associated with the given key. In addition, it can also set the corresponding key value without a given key in the dictionary. >>> D ={}>> print d. setdefault ('name', 'qiqi') qiqi >>> d {'name': 'qiqi'} (11) the updateupdate method can use a dictionary item to update another dictionary (12) values and itervaluevalues to return the value in the dictionary in the form of a list.