Tutorial on setdefault() method of dictionary in Python

高洛峰
Release: 2017-02-10 10:12:31
Original
1474 people have browsed it

When learning python dictionary operation methods, many students feel that the setdefault() method is more difficult to understand than other basic dictionary operation methods, so I thought of summarizing the following. The following article mainly introduces Python dictionaries to you. The setdefault() method, friends in need can refer to it, let’s take a look below.

Preface

As mentioned in the basic knowledge of Python, a dictionary is a variable data type, and its parameters are key pairs. The setdefault() method is similar to the dictionary's get() method in some places, and both can get the value corresponding to a given key. But the setdefault() method can set the corresponding value for a given key when the given key is not included in the dictionary.

The setdefault method prototype of Python dictionary is as follows:

dict.setdefault(key, default=None)
Copy after login

If the given key is in the dictionary, return the If the value is not in the dictionary, insert the key into the dictionary and set the value to the specified default parameter. The default value of default is None.

Using the setdefault method is equivalent to the following operation:

if key in dict:
 reurn dict[key]
else:
 dict[key] = default
 return default
Copy after login

This method is somewhat similar to the get method of the dictionary. But there are some differences. The dict.get and dict.setdefault methods can return the value when the key exists in the dictionary, and can also return the default value when the key is not in the dictionary. The difference between the two methods is that when the key is not in the dictionary, the setdefault method will insert the default key value into the dictionary and return it, while the get method only returns the default value and does not insert a new key into the dictionary.

Example:

>>> dct = {}
>>> dct
{}
>>> dct["name"] = "huoty"
>>> dct
{'name': 'huoty'}
>>> dct.setdefault("name", "esenich")
'huoty'
>>> dct
{'name': 'huoty'}
>>> dct.setdefault("fname", "esenich")
'esenich'
>>> dct
{'name': 'huoty', 'fname': 'esenich'}
>>> dct.setdefault("addr")
>>> dct
{'name': 'huoty', 'fname': 'esenich', 'addr': None}
>>> dct.get("name", "xxx")
'huoty'
>>> dct
{'name': 'huoty', 'fname': 'esenich', 'addr': None}
>>> dct.get("age")
>>> dct
{'name': 'huoty', 'fname': 'esenich', 'addr': None}
>>> dct.get("age", 2)
2
>>> dct
{'name': 'huoty', 'fname': 'esenich', 'addr': None}
Copy after login

For more tutorials on the setdefault() method of dictionaries in Python, please pay attention to PHP Chinese net!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!