Python Dictionary dict Operation definition

Source: Internet
Author: User

The dictionary is represented by curly braces {}, which is the most flexible built-in data type in Python. It is an unordered collection that accesses values by key and cannot be indexed.

Creation and use of dictionaries

The composition of a dictionary: A dictionary consists of curly braces {} to contain its data, the curly braces contain the key and its corresponding value, and a pair of keys and values becomes an item. Keys and values are separated by a colon: a comma, a number between items and items. An empty dictionary is a curly brace that does not contain any items, such as {}, which is an empty dictionary.

For example: {' name ': ' Wangyao ', ' Age ': 18}, {' Host ': ' HH ', ' Port ': 80}

Note: The key to the dictionary must be an immutable data type, and if you use tuples for keys, you must ensure that the pairs in the tuple are immutable types as well. mutable data types cannot be keyed.

Accessing values in a dictionary

Dictionaries are unordered and cannot be looked up in the same way as index subscripts. It has its own unique approach, which is to find it by key.
>>> variable name [key name]
Value

Add to

Dictionaries are added in different ways: Dictionary variable name [newly added key name] = value corresponding to new key

Modify

Dictionary modification format: dictionary variable name [to modify the value corresponding to the key name] = new value

Delete

There are three common ways to delete a dictionary, and the function is different. The following is a brief introduction to the format of these methods, the specific role and skills of the method in-depth study will be introduced.
1, del method: delete the corresponding value of the key, Del space variable name [key name], if the only write variable name is to delete this dictionary
2, Clear method: Empty the dictionary content, variable name. Clear ()
3, Python pop method: Delete the value corresponding to the key, but it will be the value of the output should be deleted after the

PS: Get all keys (key) with the keys () function, get all values (values) with the values () function, get all the key-value pairs with the items () function

You can also use for to traverse the keys or values inside the

Python Dictionary dict Operation definition

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.