Python Dictionay (dictionary) Basic usage

Source: Internet
Author: User

  • Dict (dictionary) is a collection of unordered objects, consisting of key-value pairs, which are obtained by reading a key, with variable, unordered, heterogeneous, and nested properties.

  • Dict initialization
    1, directly using the dictionary format


    2, using Dict (), [note]: The key does not add '


    3, the use of tuple (tuple)


    4, use Dict.fromkeys (), return the dictionary, the method has two parameters, parameter 1 is a list of keys, parameter 2 is the initial value of these keys, the default is empty



  • Dict Add, Delete, modify

    1, key if not present add key-value, if present, modify key-value


    2. Delete the specified key-value pair


    3. Delete the specified key-value, return the value of the delete key, or none if the specified key does not exist


    4. Delete key-value of entire dictionary


    5. Update the dictionary with Dict.update (), update if the key is the same, add if the key does not exist


    6. Use Dict.setdefault () to set the default value of the key and return the value. If the key exists, returns the original value of the key, or, if the key does not exist, adds a key-value, which returns the value


  • Dict's Query

    1. Specify key


    2. Using the Dict.items () method, returns a list of all key-value pairs (items), with key-value pairs expressed in tuple (tuple) Form


    3. Use the Dict.keys () method to return the list of all keys


    4. Use the Dict.values () method to return a list of values for all keys


    5. Use the For loop to traverse the dictionary


    6, use the Dict.get () method, if the specified key exists, then return the wisdom of the key, if not present, return the value given in the parameter (the second argument)


    7. Use Len () to return the number of key-value pairs in the dictionary


    8. Use Dict.has_key () if the key exists returns true if the key does not exist return False (python3.x no Dict.has_key ())


  • Dict Other common methods

    1, using copy.copy () shallow copy


    2. Use Copy.deepcopy () deep copy


    3, using dict.copy () shallow copy

Python Dictionay (dictionary) Basic usage

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.