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