Dictionary methods in Python

Source: Internet
Author: User

1.clear

The clear method clears all items in the dictionary. No return value

D = {'age''name':'Gumby'} D.clear ()
>>> D
{}

2.copy

The Copy method returns a new dictionary with the same key-value pairs

D = {'age''name' 'Gumby'  = d.copy ()
>>> b
{' Age ':, ' name ': ' Gumby '}

3.fromkeys

The Fromkeys method creates a new dictionary with a sequence of key values, with each key value corresponding to none by default

>>> {}.fromkeys (['name','age']) {'  Age'name' : None}

4.get

The Get method is a more permissive way to access a dictionary entry, which returns none by default when accessing a key that does not exist in the dictionary.

D = {}>>>print (d.get ('name')) None

5.items

The items method returns the dictionary all items as a sequence, note: No specific order is returned

D = {'name'Gumby' age' : >>>d.items [(' age', "), ('name ' ' Gumby ')]

6.pop

The Pop method removes the key-value pair from the dictionary and returns the value of the key

D = {'a'b ': 2}>>> d.pop ('A  ')1>>> d{'b' : 2}

7.update

The Update method can update another dictionary with one dictionary entry, if none is added, and if there is an update

 d = { " x   ": 1, "  y   ": 2,  '   Z   ": 3} b  = {  " x   " : 0} d.update (b)  >>> d{  " x  " : 0,  " y  " : 2, Span style= "COLOR: #800000" > " z   ' : 3} 

8.values

The values method returns all value in the dictionary as a list

D = {'x'y'z' : 3} >>> d.values () [1, 2, 3]

From the beginning Python from novice to Professional [move]magnus Lie Hetland

Dictionary methods in Python

Related Article

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.