Python3 Dictionary of Learning

Source: Internet
Author: User
Tags iterable

Dictionary:

Simple dictionary:

DIC = {1: ' A ', 2: ' B ', 3: ' C '}

Nested dictionaries: Dictionaries can contain tuples, lists, dictionaries

DIC = {1: ' A ', 2: ' B ', 3: ' C ', 4:{1: ' A ', 2: ' B ', 3: ' C '},5:[1,2,3],6: (5,6)}


Dic.clear () empty

Dic.fromkeys (iterable, Value=none,/) don't understand what's the use

Dic.items () Displays all key-value pairs in the dictionary

Dic.pop (K[,d]) popup K for key, must be specified, return the popup value;d is the default, when key does not exist the return value is default, if you do not specify default will error

Dic.setdefault (K[,d]) sets the default value, K represents key,d for default, and when key does not specify a value, the value is the default

Dic.values () Displays all value, does not display key

Dic.copy () copy dictionary, with new ID, generally get new dictionary by Dic1 = Dic.copy ()

Dic.get (K[,d]) gets the key value K for key,d represents default and returns Default,default default is None when key does not exist

Dic.keys () Displays all keys, does not display value

Dic.popitem () Popup key value pair, from left to right pop up, cannot specify, return value is popped key,value

Dic.update ([E,]**f) update, E is a dictionary or iterable, the value of key is updated when key is present, and a new key,value is appended when it can not exist.


The above common have get update copy


Print:

For key in DIC:

Print (Dic[key])

The key is traversed, and when value is a list or dictionary, it can be nested through a for loop

Python3 Dictionary of Learning

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.