Features of Dict
1: Fast Search speed
2: Wasted Space
3:key can not be repeated, and not variable
4: Data disordered emissions
The first feature of Dict is that the search speed is fast, regardless of whether the dict has 10 elements or 100,000 elements, the search speed is the same. The search speed of the list decreases as the element increases.
However, dict search speed is not without cost, dict the disadvantage is that the memory is large, but also waste a lot of content, the list is just the opposite, the memory is small, but the search speed is slow.
Because Dict is located by key, in a dict, key cannot be duplicated.
The second feature of Dict is that the stored key-value sequence pair is not in order! This is not the same as list:
D = {
' Adam ': 95,
' Lisa ': 85,
' Bart ': 59
}
When we try to print this dict:
>>> Print D
{' Lisa ': $, ' Adam ': Up, ' Bart ': 59}
The order of printing is not necessarily the order in which we are created, and the order in which different machines are printed may be different, which means that the dict interior is unordered and cannot be stored in an ordered collection with Dict.
The third feature of Dict is that elements that are key must be immutable, and the basic types of python, such as strings, integers, and floating-point numbers, are immutable and can be used as keys. But the list is mutable and cannot be a key.
What errors will be reported when trying to try list as key.
Immutable this limit is only used for key,value whether the variable does not matter:
{
' 123 ': [1, 2, 3], # key is Str,value is List
123: ' 123 ', # key is Int,value is str
(' A ', ' B '): True # key is a tuple, and each element of a tuple is an immutable object, value is a Boolean
}
The most commonly used key is also a string, because it is most convenient to use.
Key type: string, shape, float, tuple but not list
Value type: All can
Features of Python dict