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