Python 0 Basic Primer-------dict Dictionary table

Source: Internet
Author: User

Dict dictionary table belongs to the mapped data type

Feature: Access to data by key (key) instead of subscript

An unordered collection containing arbitrary objects

Variable length, can be nested arbitrarily

belongs to the "Variable mapping" category

Object reference table (Hash table)

Declaration: D = {} is an empty dictionary table. The dictionary table is a form of {key:value}. A key can only be an immutable element (a mutable type list cannot).

From the picture below you can also see that the Dict dictionary table is an unordered collection. is sorted by the internal hash algorithm.

Key can only be data of immutable type. A mutable type throws an error message

Accessing elements of the Dict dictionary table

cannot be accessed through an index. Otherwise it will be an error. Access to Dict[key]. Key does not exist and throws an error

Access a multi-level dict dictionary table.

Get Value through Get (key). The advantage of get access is that it does not throw an exception when the Key does not exist, but instead returns a none

The parameters after the Get method can be set to the default value returned when not found

Common operations

(1) dict () built-in function, converted to dict dictionary table "When key does not specify a type, the default is string"

(2) Fromkeys (keys) converts a set of data into a key for the Dict dictionary table

(3) in to determine if Key exists

(4) keys () Gets all keys for the Dict dictionary table

(5) VALUES () Gets the value in the Dict dictionary table

(6) items () Gets all the items that return an array of type tuples

(7) Len (dict) Gets the length of the Dict dictionary table

(8) Copy () Duplicate Dict dictionary table

(9) Clear () Empty dict dictionary table

(10) Modify information: Direct assignment or using the update () method

(11) Delete the Del and Pop () method. The Pop () method is to delete the Key:value and return the value values

Pop () method the second parameter returns the value if the deleted Key:value does not exist. Do not use the second parameter, delete the Key:value does not exist will throw an exception

() Popitem () deletes and returns an element of the first tuple type.

Attached: Loop output

Attached: dict dictionary table with function

Python 0 Basic Primer-------dict Dictionary table

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.