PythonDictionarysetdefault
popitem

setdefaultDictionary

clear

Returns the value of a key, inserting a default if the key is not present.

Syntax

dict.setdefault(key, default?)

Example

Enter values below to update the example in real time.

person
setdefault
print
Alice
person = {"name": "Alice"}
person.setdefault("age", 0)
print(person)  # {'name': 'Alice', 'age': 0}

person.setdefault("name", "Bob")  # 이미 있으므로 무시
print(person["name"])  # Alice