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