Level 10 : What happens when trying to access a missing key with d.setdefault(k, v)?


d = {'a': 1, 'b': 2}
print(d.setdefault('c', 3))
print(d)

Author: SandhyaR | Tags: dictionary | 1859+ Python FAQs | Share
destructor | type-conversion-and-casting | id-function | higher-order-function | collection | exception-handling | functions | comprehension | int | functional-programming | default-mutable-argument | global | exponentiation | datatype | introduction-to-multiprocessing | file | lambdas | arithmetic-expression | array | assignment-operators | for | conditional-statement | error-handling | arbitrary-arguments-(*args,-**kwargs) | encoding | immutability | function-and-scope | built-in-modules | built-in-function | import-method | boolean-masking | dictionary-comprehensions | generator | identifier | binary | heap-memory | statement | single-inheritance | reading:-read(),-readline(),-readlines() | comparison | iterable | syntax-error | string-formatting | error-handling | creating-and-starting-threads | classes | set-operation | arithmetic-operation | creating-custom-modules | string-iteration | generators | sorted | regular-expressions | format | staticmethod | syntax-error | syntaxerror | syntax | __all__-attribute | recursion | sys | sum() | split | using-else-and-finally | string-methods | thread-synchronization | tuples | data_structure | regex | default-arguments | key-value-pairs | strings | rollback | working-with-dates-and-times | dictionaries | test-discovery | zip | with-statement | class-decorators | collections | string-method | threading | loop-control | parameter | yield | type_conversion | thread-safe-data-structures-(queue.queue) | lists | methods | reading-and-writing-files | string-concatenation | sorting | operations | else-statement | mysql | string | set | math | logical-operator | list-methods-and-slicing | managing-directories-with-os-and-shutil | objects | conditional-statement | numpy | complex-number | else | __future__ | raise | python-module | map-function | command-line-parsing | indexing:-loc-vs-iloc | logical-operation | multiple-inheritance | assert | bitwise | pickling--unpickling | file-handling | nested-loop | built-in | factorial-function | operator-overloading | aggregating-data-with-functools.reduce() | operators | expand-tab | print | docstring | regular-expression | zip-function | using-assertions-(assertequal,-assertraises) | frozenset | access-modifier | expression | using-yield-in-functions | boolean | application | escape-operator | choice | writing:-write(),-writelines() | list-method | context-manager | list-mutation | class | method | round-function | shallow-copy | bitwise-operator | basic | using-join()-and-is_alive()-methods | string-formatting | introduction-to-python | range | overflow | function | string-manipulation | join | counter | static-method | datetime.date,-datetime.time,-datetime.datetime | keyword | intering | type-conversion | generators-in-python | def | assignment-operator | memory | implementation | class-and-instance-variable | asyncio-module | identity-operator | package-management | operator | call | boolean-and-non-boolean | while-loops | variable | scientific-notation | indentation | sum-function | error | precedence | memory-management | membership-operators-(in,-not-in) | floating-point-precision | lambda-function | using-the-threading-module | loop | mutable | center | literal-characters-and-special-characters | python-syntax-and-structure | input-function | data-structures-in-python | thread | data-structures | interpreter | gil | sorting-and-ranking | match | loop-control-statements | walking-through-directories-using-os.walk() | built-in-function | list,-set,-and-dictionary-comprehensions | advanced-file-operations | python-operators | filter | filter-function | class-and-object | goto-function | list-slicing | quantifiers-(*,-+,-?,-{}) | formal-and-actual-parameter | installation | list | postfix | attribute | filtering-and-indexing | return-statement | stack | list-comprehensions | string-slicing | tuple | integer | namedtuple | directory-operations | counter-function | virtual-environment | time-series-with-pandas | list-vs-tuple | binary-operation | bitwise-operator | design-principle | data-types | namespace | inherit | decimal | joining-and-merging-dataframes | turtle | iterator | print-statement | math-copysign | async | method-overloading | list-indexing | class-and-inheritance | precedence-and-associativity | working-with-file-paths | fabs-function | multithreading | software-design | working-with-binary-files-(rb,-wb) | invalid-list | identity-operators-(is,-is-not) | using-os-and-pathlib-modules | logical-operator | virtual-environment | benefit | comment | control-statement | unit-testing | append | data-type | recurssion | reading-and-writing-large-files-efficiently | exception-handling | booleans | input-function | debugging | defining-functions | loops | matplotlib | global-variable | anchors-(^,-$) | zfill-function | data-structure | multiple-inheritance | dicts | functional-programming | slicing | formatting-with-strftime | while-and-for-loop | conversion | complex | asyncio | touples | while-else-loop | list-comprehension | min-max-function | version-check | operator-or | data-structure | difference-between-threads-and-processes | input | inheritance | deep-copy | mathematical-function | compute | matrix-list-comprehension | head-and-tail | dictionary | condition | list-operation | sys-module | swapping | dataframe-and-series-objects | name-mangling | python | copy | while-loop | list-comprehension | multiple-except-blocks | control-flow | logical-operators | magic-method | integers | control-flow | datetime | max-min | multiprocessing | iterators | pep | floats | constructor | round | polymorphism | update | decorator | using-map()-for-transformations | using-lists-as-stacks | module | introduction-to-threads | numeric | min-function | slice | why-use-multiprocessing-over-threading?-(gil) | encapsulation | global-variable | mapping-function | function-argument | data-type | matrix | odd-number | global-keyword | operand | overriding-methods | enumerate() | typeerror | handling-missing-values-(fillna,-dropna) | working-with-temporary-files-using-tempfile | lambda-functions | conditional-statements | data-type-conversions | tuple-methods | oops | indexing | random | descriptors | file_handling | string-method | init | raise-statement | feature | string-slicing | dictionary-methods | exception | python-repl | pass | type-conversion | oop | library | object-attribute | tuple-unpacking | pandas | parsing-with-strptime | pseudocode | opening-files-(open()) | operator-and | identity-operator | lambda | version | immutable-data-type | python_version | sets | random-function | python-history | list-join | file-handling | __init_subclass__ | mathematics | python-django ...