Skip to content

Instantly share code, notes, and snippets.

Last active Jul 11, 2020
What would you like to do?
#setdefault - if key is not there , will add the key with value none
person = {'name': 'karthi', 'age': 7}
city = person.setdefault('city')
#Output:{'name': 'karthi', 'age': 7, 'city': None}
#setdefault- if key is not there , will add key and value specified
person = {'name': 'karthi', 'age': 7}
city = person.setdefault('city','chennai')
#Output:{'name': 'karthi', 'age': 7, 'city': 'chennai'}
#setdefault-> if key exists, returns the value of that specified key
person = {'name': 'karthi', 'age': 7}
print(person.setdefault('age',10)) #Output:7
print (person)
#Output:{'name': 'karthi', 'age': 7}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment