Features of dict in Python, update dict, traversal dict

Source: Internet
Author: User

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 ': +,    ' Lisa ': $,    ' 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:

{     # key is Str,value is List    123: ' 123 ',  # key is Int,value is str    (' A ', ' B '): True  # key is T Uple, 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.

Python Update dict

Dict is mutable, meaning that we can add new Key-value to dict at any time. For example, there are dict:

D = {    ' Adam ': +,    ' Lisa ': $,    ' Bart ': 59}

To add a new classmate ' Paul ' score 72, use the assignment statement:

>>> d[' Paul '] = 72

Then look at the contents of Dict:

>>> print d{' Lisa ': $, ' Paul ': The ' Adam ': $, ' Bart ': 59}

If the key already exists, the assignment replaces the original value with the new value:

>>> d[' bart ' = 60>>> print d{' Lisa ': $, ' Paul ': "The ' Adam ':" The ", '" Bart ': 60}
Task

Please update the following dict according to Paul's score 72:

D = {

: ' Adam ',

A: ' Lisa ',

A: ' Bart '

}

Reference code:

D = {
: ' Adam ',
A: ' Lisa ',
A: ' Bart '
}
D[72]= ' Paul '

Python Traversal dict

Since Dict is also a collection, traversing the dict is similar to traversing the list and can be implemented through a for loop.

Use the For loop directly to traverse the Dict key:

>>> d = {' Adam ': +, ' Lisa ': $, ' Bart ':}>>> for key in D: ...     Print Key ... Lisaadambart

Because the corresponding value can be obtained by key, the value can be obtained in the loop body.

Task

Please use the For loop to traverse the following dict to print out the name:score .

D = {    ' Adam ': +,    ' Lisa ': $,    ' Bart ': 59}
Reference code:
D = {
' Adam ': 95,
' Lisa ': 85,
' Bart ': 59
}
For key in D:
Print key + ': ', D[key]

Features of dict in Python, update dict, traversal 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.