Python3 Dict Incomplete Summary

Source: Internet
Author: User

Make an incomplete summary of the dictionaries in Python3.

There is no order in the dictionary.

>>> d={"Soam": 2,'Ham': 1,'eggs': 3}>>> list (D.keys ())#returns the key list for the dictionary['Soam','Ham','eggs']>>> list (D.items ())#returns a list of (Key,value) dictionaries[('Soam', 2), ('Ham', 1), ('eggs', 3)]>>> list (d.values ())#returns the value list of a dictionary[2, 1, 3]>>> D.update ({'Gyj': 4})#Merging of Dictionaries>>>d{'Soam': 2,'Ham': 1,'eggs': 3,'Gyj': 4}>>> forKeyinchD:#Dict can be iterated directly, unlike Java       Print(Key,'\ t', D[key]) Soam2Ham1eggs3Gyj4

The keys in Dict can only be immutable objects.

Four ways to create Dict

>>> {'name':'Gyj',' Age': 45}{'name':'Gyj',' Age': 45}>>> d={}>>> d['name']='Gyj'>>> d[' Age']=45>>>d{'name':'Gyj',' Age': 45}>>> Dict (name='Gyj', age=45){'name':'Gyj',' Age': 45}>>> Dict ([('name','Gyj'),(' Age', 45)]){'name':'Gyj',' Age': 45}>>> Dict.fromkeys (['a','b'],0) {'a': 0,'b': 0}

The following are the ways to create dictionaries that are only supported by Python3:

>>> dict (Zip (['name','age'],['gyj  ){'name''gyj')  'age': 45}

Python3 dict Incomplete summary

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.