Built-in dictionary functions commonly used in python

Source: Internet
Author: User

1. len (mapping) returns the ing length (number of key-value pairs)

2. hash (obj) returns the hash value of obj.

>>> myDict = {'name':'earth', 'port':'80'}>>> len(myDict)2>>> hash('name')15034981

3. dict. copy () returns a copy of the Dictionary (light copy ).

>>> myDict = {'name':'earth', 'port':'80'}>>> yourDict = myDict.copy()>>> yourDict{'name': 'earth', 'port': '80'}>>> id(myDict)41816664L>>> id(yourDict)41819544L

4. dict. clear () deletes all elements in the dictionary.

>>> myDict.clear()>>> myDict{}


5. dict. fromkeys (seq, val = None)

Create and return a new dictionary. Use the elements in seq as the dictionary key, and val as the initial values corresponding to all keys in the dictionary (if this value is not provided, the default value is None ).

>>> seq = ['name', 'port']>>> myDict.fromkeys(seq){'name': None, 'port': None}


6. dict. get (key)

Return the value corresponding to the key in the dictionary dict. If the key does not exist in the dictionary, the default value is returned (note that the default value of the parameter default is None ).

>>> myDict = {'name':'earth', 'port':'80'}>>> myDict.get('name')'earth'>>> print myDict.get('home')None


7. dict. items () returns a list of key-value pairs in the dictionary.

>>> myDict.items()[('name', 'earth'), ('port', '80')]


8. dict. keys () returns a list containing the dictionary's middle keys.

9. dict. values () returns a list containing all values in the dictionary.

>>> myDict.keys()['name', 'port']>>> myDict.values()['earth', '80']


10. dict. iter ()
Methods iteritems (), iterkeys (), and itervalues () are the same as their non-iterative methods. The difference is that they return an iterator instead of a list.

11. dict. pop (key [, default])
Similar to the get () method, if the key in the dictionary exists, delete it and return the dict [key]. If the key does not exist and no default value is given, a KeyError exception is thrown.

>>> myDict.pop('port')'80'>>> myDict{'name': 'earth'}>>> myDict.pop('port', 'No such key!')'No such key!'


12. dict. setdefault (key, default = None)
Similar to the method set (), if the dictionary does not contain a key, the value is assigned by dict [key] = default.

>>> myDict.setdefault('port', '8080')'8080'>>> myDict{'name': 'earth', 'port': '8080'}


13. dict. update (dict2) adds the key-value pairs of the dictionary dict2 to the dictionary dict.

>>> yourDict = {'language':'Python'}>>> yourDict{'language': 'Python'}>>> myDict.update(yourDict)>>> myDict{'name': 'earth', 'language': 'Python', 'port': '8080'}


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.