Answers for "python dictionary get vs setdefault"

0

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
Posted by: Guest on October-09-2021

Python Answers by Framework

Browse Popular Code Answers by Language