Getting Started with Python--dict

Source: Internet
Author: User

I. DICT features


1. Find Fast: The search speed is the same regardless of whether the dict has 10 elements or 100,000 elements. The search speed of the list decreases as the element increases.

Cons: Take up memory, waste content, and list just the opposite, take up small memory, but the search speed is slow.
Because Dict is located by key, in a dict, key cannot be duplicated.

2. 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 were created, and the different machine print order may be different, which indicates that the dict interior is unordered and cannot be stored in an ordered collection with Dict.

3. As key elements must be immutable, Python's basic types such as strings, integers, floating-point numbers are immutable and can be used as keys.
But the list is mutable and cannot be a 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.

Two. Update Dict

Dict is variable and can be added at any time in the network Dict Key-value
D = {
' Adam ': 95,
' Lisa ': 85,
' Bart ': 59
}
Add a new classmate ' Paul ' score 72, with an assignment statement
>>>d[' Paul '] = 72
View Dict content again:
>>> Print D
{' Lisa ': $, ' Paul ': $, ' Adam ': Up, ' Bart ': 59}
If key already exists, replace the original value with the new value

Three. Traverse Dict
Use the For loop directly to loop the Dict key:
>>> d = {' Adam ': $, ' Lisa ': $, ' Bart ': 59}
>>> for K,v in D.items ():
... print k, ': ', V
...
Lisa:85
Adam:95
bart:59

This article is from the "11732728" blog, please be sure to keep this source http://11742728.blog.51cto.com/11732728/1813851

Getting Started with 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.