Features of Python Dict

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.