Create:
Dict = {' Name ': ' Zara ', ' age ': 7, ' Class ': ' First '};
Delete:
Del dict[' Name ']; # Delete key is ' Name ' entry dict.clear (); # Empty dictionary all Entries del dict; # Delete Dictionary
Properties of Dictionary Keys
A dictionary value can take any Python object without restriction, either as a standard object or as a user-defined one, but not a key.
Two important points to keep in mind:
1) The same key is not allowed to appear two times. When created, if the same key is assigned a value of two times, the latter value is remembered
2) The key must be immutable, so it can be used as a number, a string, or a tuple, so you can't use a list.
Dictionary built-in functions & methods
There are many, specifically see the manual
The Python dictionary contains the following built-in functions:
Serial Number |
Functions and descriptions |
1 |
CMP (Dict1, DICT2) Compares two dictionary elements. |
2 |
Len (dict) Calculates the number of dictionary elements, that is, the total number of keys. |
3 |
STR (DICT) The output dictionary is a printable string representation. |
4 |
Type (variable) Returns the type of the variable entered and returns the dictionary type if the variable is a dictionary. |
The Python dictionary contains the following built-in methods:
Serial Number |
Functions and descriptions |
1 |
Radiansdict.clear () Delete all elements in a dictionary |
2 |
Radiansdict.copy () Returns a shallow copy of a dictionary |
3 |
Radiansdict.fromkeys () Create a new dictionary with the keys to the dictionary in sequence seq, Val is the initial value corresponding to all keys in the dictionary |
4 |
Radiansdict.get (Key, Default=none) Returns the value of the specified key if the value does not return the default value in the dictionary |
5 |
Radiansdict.has_key (Key) Returns False if the key returns true in the dictionary Dict |
6 |
Radiansdict.items () Returns an array of traversed (key, value) tuples as a list |
7 |
Radiansdict.keys () Returns a dictionary of all keys in a list |
8 |
Radiansdict.setdefault (Key, Default=none) Similar to get (), but if the key does not exist in the dictionary, the key is added and the value is set to default |
9 |
Radiansdict.update (DICT2) Update the key/value pairs of the dictionary dict2 to the Dict |
10 |
Radiansdict.values () Returns all values in the dictionary as a list |
Python's Dictionary dictionary