Scroll Down to See All▾
absaiterallanextanyasciibinboolbreakpointbytearraybytescallablechrclassmethodcompilecomplexdelattrdictdivmodenumerateevalexecfilterfloatformatfrozensetgetattrglobalshasattrhashhelphexidinputintisinstanceissubclassiterlenlistlocalsmapmaxmemoryviewminnextobjectoctopenordpowprintpropertyrangereprreversedroundsetsetattrslicesortedstaticmethodstrsumsupertupletypevarszip__import__clear_(dict)clear_(list)clear_(set)copy_(dict)copy_(list)copy_(set)fromkeysgetitemskeyspop_(dict)pop_(list)pop_(set)popitemupdatevaluescount_(tuple)count_(list)count_(str)index_(tuple)index_(list)index_(str)adddifferencedifference_updatediscardintersectionintersection_updateisdisjointissubsetissupersetremove_(set)remove_(list)symmetric_differencesymmetric_difference_updateunionupdateclosefilenoflushisattyreadreadablereadlinereadlinesseekseekabletelltruncatewritablewritewritelinesappendextendinsertreversesortcapitalizecasefoldcenterencodeendswithexpandtabsfindformatisalnumisalphaisasciiisdecimalisdigitisidentifierislowerisnumericisprintableisspaceistitleisupperjoinljustlowerlstripmaketranspartitionreplacerfindrindexrjustrpartitionrsplitrstripsplitsplitlinesstartswithstripswapcasetitletranslateupperdirzfillFunction Details: setdefault
Description
Inserts key with a value of default if key is not in the dictionary.
Extended Description
The setdefault() method returns the value of the specified key. If the key does not exist, it inserts the key with the specified default value and returns the default value. If the key already exists, it returns its value without modifying the dictionary. This method is particularly useful when you want to initialize a key with a default value if it doesn't exist, and then use or modify that value.
Function Signature
dict.setdefault(key: Any, default: Any = None) -> Any
Module: dict
Class: dictionary
Parameters
Parameter List
- key: Any
- default: Any
Return
Returns the value of the specified key. If the key does not exist, it returns the default value.
Return Type
Any
Output
Explanation
This example shows how setdefault() works with existing and new keys.