Level 4 : What is the output of the following code?


from collections import defaultdict

data = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple']
counts = defaultdict(int)

for item in data:
    counts[item] += 1

print(counts)

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