| Method |
Example |
Description |
| Dict.clear () |
>>> Phonebook
{' Lily ': ' 2345 ', ' Tom ': ' 1234 '}
>>> Phonebook.clear ()
>>> Phonebook
{} |
Clears all the entries in the dictionary, with no return value (or none returned). This is an in-place operation where the original dictionary is manipulated directly. |
| Dict.copy () |
>>> x = {' A ': 1, ' B ': 2, ' C ': 3}
>>> y = x.copy ()
>>> y[' C ']=100
>>> y
{' A ': 1, ' C ': +, ' B ': 2}
>>> x
{' A ': 1, ' C ': 3, ' B ': 2} |
Returns a new dictionary with the same key-value pairs.
But this method is shallow copy. The original dictionary is not affected when the value is substituted in the copy (the re-assignment in the example). But if you modify a value (not a replacement, a modification, such as a delete), the original dictionary will change. |
| Deepcopy (Dict) |
|
This is a deep copy, which is different from copy. When a new copy is obtained, the modification does not affect the original dictionary. |
| Dict.fromkeys () |
>>> x= {' ab ': ' 1 ', ' CD ': ' 2 '}
>>> X.fromkeys ([' EF ', ' gh '])
{' EF ': none, ' GH ': none}
>>> x
{' AB ': ' 1 ', ' CD ': ' 2 '}
>>> Y=x.fromkeys ([' EF ', ' gh '])
>>> y
{' EF ': none, ' GH ': none} |
Creates a new dictionary with the given key, with each key corresponding to a default value of None.
Returns a new sequence.
can {}.fromkeys (...) or Dict.fromkeys (...) |
| Dict.get () |
>>> x
{' AB ': ' 1 ', ' CD ': ' 2 '}
>>> x.get (' CD ')
' 2 '
>>> x.get (' AA ')
>>> print x.get (' AA ')
None
>>> x.get (' AA ', ' N/A ')
' N/A ' |
The method that accesses the dictionary entry, if it does not exist, returns none. You can also define the return value when you cannot find it yourself. |
| Dict.has_key () |
>>> x
{' AB ': ' 1 ', ' CD ': ' 2 '}
>>> x.has_key (' AB ')
True
>>> x.has_key (' ax ')
False |
Check if the dictionary contains a specific key.
This expression is equivalent to K in D |
| Dict.items () |
>>> x
{' AB ': ' 1 ', ' CD ': ' 2 '}
>>> X.items ()
[(' Ab ', ' 1 '), (' CD ', ' 2 ')] |
Returns all entries for the dictionary as a list, with each item in the list represented as a (key, value) pair, but returned in no particular order. |
| Dict.iteritems () |
>>> x
{' AB ': ' 1 ', ' CD ': ' 2 '}
>>> it = X.iteritems ()
>>> List (IT)
[(' Ab ', ' 1 '), (' CD ', ' 2 ')] |
Similar to the Ltems, but returns an iterator |
| Dict.keys () |
>>> x
{' AB ': ' 1 ', ' CD ': ' 2 '}
>>> X.keys ()
[' AB ', ' CD '] |
Returns the key of the dictionary as a list |
| Dict.pop () |
>>> x
{' AB ': ' 1 ', ' CD ': ' 2 '}
>>> x.pop (' AB ')
' 1 '
>>> x
{' CD ': ' 2 '} |
Gets the value corresponding to the given key, and then removes the key-value pair from the dictionary. |
| Dict.popitem () |
>>> x
{' GH ': ' 3 ', ' AB ': ' 1 ', ' CD ': ' 2 '}
>>> X.popitem ()
(' GH ', ' 3 ')
>>> x
{' AB ': ' 1 ', ' CD ': ' 2 '} |
Functions like pop, just a random entry in the popup list. |
| dict.setdefault |
>>> x
{' AB ' : ' 1 ', ' CD ': ' 2 '}
>>> print x.setdefault (' gh ')
None
>>> x
{' GH ': None, ' AB ': ' 1 ', ' CD ': ' 2 '}
>>> print x.setdefault (' ij ', ' n/a ')
N/a
>>> x
{' GH ': None, ' AB ': ' 1 ', ' ij ': ' N/A ', ' CD ': ' 2 '}
>>> x.setdefault (' ab ')
' 1 ' |
To some extent similar to the Get method.
When the key does not exist, SetDefault returns the default value and the corresponding update dictionary, the default value is optional, you can set the value yourself. If the key exists, it returns its corresponding value, but does not change the dictionary. |
| Dict.update () |
>>> y
{' mm ': ' Haha ', ' CD ': ' KK '}
>>> x
{' GH ': None, ' ab ': ' 1 ', ' ij ': ' N/A ', ' CD ': ' 2 '}
>>> x.update (y)
>>> x
{' AB ': ' 1 ', ' mm ': ' Haha ', ' CD ': ' KK ', ' ij ': ' N/a ', ' GH ': None} |
Use a dictionary entry to update another dictionary. The items in the provided dictionary are added to the old dictionary, and if the same key is overwritten |
|
D.values () |
>>> x
{' AB ': ' 1 ', ' mm ': ' Haha ', ' CD ': ' KK ', ' ij ': ' N/a ', ' GH ': None}
>>> x.values ()
[' 1 ', ' haha ', ' kk ', ' N/A ', None] |
Returns a list of values in a dictionary that can contain duplicate elements in a list of returned values. |