Python‎ > ‎dict‎ > ‎

dict.setdefault()


Introduction
The method add the keys and values to the dictionary.

Syntax
dict.setdefault(key, value)

Parameters
key & Value

Examples
The method returns with:
 >>> 
>>> a=dict({'Din': 4, 'John': 2, 'Adil': 3, 'Fen': 1})
>>> a.setdefault('Erica', 7)
7
>>> a
{'Din': 4, 'John': 2, 'Erica': 7, 'Adil': 3, 'Fen': 1}
>>> a.setdefault('Lee',10)
10
>>> a
{'Lee': 10, 'Din': 4, 'John': 2, 'Erica': 7, 'Adil': 3, 'Fen': 1}
>>> a.setdefault('Win',4)
4
>>> a
{'Lee': 10, 'Din': 4, 'John': 2, 'Erica': 7, 'Adil': 3, 'Fen': 1, 'Win': 4}
>>> a.setdefault(('Jin',3),('Al',3))
('Al', 3)
>>> a
{'Lee': 10, 'Din': 4, 'John': 2, 'Erica': 7, 'Adil': 3, 'Fen': 1, 'Win': 4, ('Jin', 3): ('Al', 3)}
>>>

Comments