Python3dict (dictionary) description

Source: Internet
Author: User
This article explains how to use python to perform operations on the dict Dictionary. Clear (clear dictionary content)

Stu = {'num1': 'Tom ', 'num2': 'Lucy', 'num3': 'Sam ',} print (stu. clear () # output: None

Copy (copy dictionary)

stu = {    'num1':'Tom',    'num2':'Lucy',    'num3':'Sam',}stu2 = stu.copy()print(stu2)

Fromkeys (specify a list and use the values in the list as the dictionary key to generate a dictionary)

Name = ['Tom ', 'Lucy', 'Sam '] print (dict. fromkeys (name) print (dict. fromkeys (name, 25) # specify the default value # output: {'Tom ': None, 'Lucy': None, 'Sam ': None} # {'Tom': 25, 'Lucy ': 25, 'Sam': 25}

Get (specify the key to obtain the corresponding value)

Stu = {'num1': 'Tom ', 'num2': 'Lucy', 'num3': 'Sam ',} print (stu. get ('num2') # output: Lucy

Items (return the list of "key-value pairs consisting of elements)

Stu = {'num1': 'Tom ', 'num2': 'Lucy', 'num3': 'Sam ',} print (stu. items () # output: dict_items ([('num2', 'Lucy '), ('num3', 'Sam'), ('num1 ', 'Tom ')])

Keys)

Stu = {'num1': 'Tom ', 'num2': 'Lucy', 'num3': 'Sam ',} print (stu. keys () # output: dict_keys (['num3', 'num1', 'num2'])

Pop (get the value of the specified key and delete it in the dictionary)

Stu = {'num1': 'Tom ', 'num2': 'Lucy', 'num3': 'Sam ',} name = stu. pop ('num2') print (name, stu) # output: Lucy {'num1': 'Tom ', 'num3': 'Sam '}

Popitem (randomly obtain a key-value pair and delete it in the dictionary)

Stu = {'num1': 'Tom ', 'num2': 'Lucy', 'num3': 'Sam ',} name = stu. popitem () print (name, stu) # output: ('num2', 'Lucy ') {'num3': 'Sam', 'num1': 'Tom '}

Setdefault (obtain the value of the specified key. if the key does not exist, it is created)

Stu = {'num1': 'Tom ', 'num2': 'Lucy', 'num3': 'Sam ',} name = stu. setdefault ('num5') print (name, stu) # output: None {'num1': 'Tom ', 'num2': 'Lucy', 'num5': None, 'num3': 'Sam '}

Update (add a key-value pair to the dictionary)

Stu = {'num1': 'Tom ', 'num2': 'Lucy', 'num3': 'Sam ',} stu. update ({'num4': 'Ben'}) print (stu) # output: {'num2': 'Lucy ', 'num3': 'Sam', 'num1 ': 'Tom ', 'num4': 'Ben '}

The above is the detailed description of python3 dict (dictionary). For more information, see other related articles in the first PHP community!

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.