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