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