Python3 dictionary SetDefault () method

Source: Internet
Author: User

The Python dictionary SetDefault () function is similar to the Get () method, and if the key does not exist in the dictionary, the key is added and the value is set to the default value.

Dict.setdefault (Key, Default=none)
  • Key--The value of the lookup.
  • Default-key is not present, set the defaults.

If the dictionary contains a given key, the value corresponding to the key is returned, otherwise the value set for the key is returned.

The following example shows how the SetDefault () method is used:

Dict = {' Name ': ' Runoob ', ' Age ': 7}print ("The value of age key is:%s"%  dict.setdefault (' age ', None)) print ("The value of the Sex key is:%s"%  Dict.setdefault (' Sex ', None)) print ("New Dictionary:", Dict) "" "The value of the age key is: The value of the 7Sex key is: None The new dictionary is: {' age ': 7, ' Name ': ' Runoob ', ' Sex ': None} "" "


Python3 dictionary SetDefault () method

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: 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.