1. Create a dictionary: each key and its value are separated by a colon, and items are separated by a comma. The entire dictionary is enclosed by a pair of braces.
2. Basic dictionary operations: len (d) returns the number of items in d. D [k] returns the value associated with the key k. D [k] = v: Associate the value v with the key k. Del d [k] deletes the key position k. K in d check whether d contains k keys.
3. formatted dictionary string: you can add a (enclosed in parentheses) Key to the % character in each conversion character, followed by the description element.
4. Dictionary Method: (1) clear method: clear all items in the dictionary. No return value.
(2) copy method: returns a new dictionary with the same key-value pairs (the original dictionary is not affected in light replication, but if a value is modified instead of replaced, the original dictionary will also change ). You can use the deepcopy function in the copy module to perform deep replication. The dictionary returned by deepcopy will not change in the future.
(3) fromkeys: Create a new dictionary using the given key. The default value of each key is None. If you do not want to use None as the default value, you can use the custom default value as the second parameter of fromkeys.
(4) get method: when you use the get method to access a key that does not exist, there is no exception, but a value of None is obtained. You can also customize the default value as the second parameter of the get method.
(5) has_key method: Check whether the dictionary contains the given key. D. has_key (k) is equivalent to k in d.
(6) items and iteritems methods: the items method returns all dictionary items in the list mode. There is no special order for items to be returned. The iteritems method works roughly the same but returns an iterator object instead of a list.
(7) keys and iterkeys Methods: similar to above. Www.2cto.com
(8) pop method: obtain the value corresponding to the given key, and then remove the key-value pair from the dictionary.
(9) popitem method: a random item is displayed.
(10) setdefault method: similar to the get method to some extent, it can obtain the value associated with the given key. In addition, setdefault can also set the corresponding key value without a given key in the dictionary (that is, update the dictionary content, but the get method does not update the content, it is only displayed to the user by default ).
(11) update method: update another dictionary using one dictionary item. Items in the provided dictionary will be added to the old dictionary. If the same key exists, it will be overwritten.
(12) values and itervalues Methods: return the values in the dictionary in the form of a list. Unlike the list of return keys, the list of returned values can contain duplicate elements.
Author: uohzoaix