Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python dictionary get vs setdefault

# setdefault will insert key with a value of default
# get will not insert key

a = {}
b = a.get('key','value')
print(a,b)
# {} value

a = {}
b = a.setdefault('key','value')
print(a,b)
# {'key': 'value'} value
 
PREVIOUS NEXT
Tagged: #python #dictionary #setdefault
ADD COMMENT
Topic
Name
4+4 =