Common ways to manipulate Python dictionaries

Source: Internet
Author: User
Tags shallow copy

A dictionary is another mutable container model and can store any type of object.

Each key value pair in the dictionary is separated by ': ', each pair is separated by ', ', and the entire dictionary is included in the {}

The key must be unique, but the value does not have to be.

The value can take any data type, but the key must be immutable, such as a string, a number, or a tuple.

To access the values in the dictionary:

Dictionary operations: Increase

Dictionary operation: Delete pop (key)

Removes a key value pair from the dictionary by specifying the key value, which returns the value of the deleted key

You must specify the key to delete, or the error

Dictionary Delete: Clear ()

Dictionary operation: Change

Dictionary operations: Check Get (), SetDefault (), Has_key ()

Get (Key): finds the corresponding value by the given key, and returns none if the given keys do not exist in the dictionary

SetDefault (Key): finds the corresponding value by a given key, returns none if the given key does not exist in the dictionary, and adds a key-value pair to the dictionary

Dictionary operation: Facelift () Set the dictionary key-value pairs, group members, and finally return as a list

Dictionary operations: Keys (), VALUES ()

Dictionary operation: Update (DICT1) updates the key-value pairs in the dictionary dict1 to the dictionary

Dictionary operation:

Items () An iterator that returns all elements of the dictionary

Keys () returns an iterator consisting of all keys in the dictionary

VALUES () returns an iterator that consists of all values in the dictionary

Fromkeys (seq): Creates a dictionary that is the key to the dictionary of elements in the sequence SEQ, and value is the initial value corresponding to all keys in the dictionary

Copy (): Shallow copy of Dictionary

Common ways to manipulate Python dictionaries

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.