Level 1 : Creating an iterator from a dictionary.


class DictIterator:
    def __init__(self, data):
        self.data = list(data.items())
        self.index = 0

    def __iter__(self):
        return self

    def __next__(self):
        if self.index >= len(self.data):
            raise StopIteration
        key, value = self.data[self.index]
        self.index += 1
        return f"{key}: {value}"

it = iter(DictIterator({"a": 1, "b": 2}))
print(list(it))

Author: SandhyaR | Tags: iterator | 1736+ Python FAQs | Share
round | regular-expression | design-principle | zip | dictionaries | destructor | type_conversion | arithmetic-expression | __init_subclass__ | staticmethod | fabs-function | data-structures | higher-order-function | round-function | pep | heap-memory | enumerate() | bitwise-operator | generators-in-python | logical-operation | decorator | print | software-design | operator | pickling-&-unpickling | file-handling | frozenset | slicing | multithreading | memory-management | asyncio | matrix-list-comprehension | zfill-function | postfix | goto-function | tuple | operators | mutable | indentation | exception | overflow | lambda-function | bitwise-operator | functional-programming | default-mutable-argument | formal-and-actual-parameter | oop | inheritance | invalid-list | oops | list-vs-tuple | match | data-type | sys | gil | polymorphism | python-history | syntax | boolean | input | math-copysign | multiple-inheritance | max-min | virtual-environment | command-line-parsing | iterator | constructor | copy | mysql | threading | min-function | control-flow | escape-operator | recurssion | variable | dicts | list-operation | list-indexing | string-method | assignment-operator | class-and-inheritance | pandas | operator-overloading | precedence | classes | lambdas | bitwise | mathematical-function | parameter | call | input-function | expand-tab | else-statement | swapping | counter-function | python_version | context-manager | string-manipulation | syntaxerror | shallow-copy | immutability | function-argument | arithmetic-operation | type-conversion | collections | comprehension | installation | sum-function | module | range | binary-operation | operator-or | attribute | global-variable | matrix | exception-handling | name-mangling | input-function | list-comprehension | string-formatting | class-and-object | generators | virtual-environment | version | with-statement | file_handling | import-method | update | logical-operator | for | error-handling | encapsulation | integer | indexing | random-function | namespace | class-and-instance-variable | recursion | random | while-else-loop | compute | array | binary | numeric | data-type | benefit | multiprocessing | boolean-and-non-boolean | application | error | condition | async | interpreter | rollback | operand | expression | global | raise | deep-copy | print-statement | version-check | file-handling | factorial-function | identity-operator | iterable | package-management | while-and-for-loop | identity-operator | string-concatenation | floating-point-precision | lambda | object-attribute | datatype | list-join | sys-module | math | global-keyword | head-and-tail | statement | join | feature | data-structure | min-max-function | identifier | split | list-comprehension | function-and-scope | numpy | generator | id-function | set-operation | __future__ | type-conversion | stack | implementation | mapping-function | data-structure | functions | loop-control | lists | immutable-data-type | loop | library | syntax-error | python-repl | inherit | string-formatting | objects | operator-and | complex | __all__-attribute | data_structure | set | datetime | namedtuple | python-module | list-slicing | yield | int | built-in | center | class | python | init | methods | precedence-and-associativity | string-method | scientific-notation | descriptors | sum() | conditional-statement | regex | exponentiation | sorting | built-in-function | nested-loop | debugging | static-method | string-slicing | operations | sorted | touples | zip-function | control-flow | mathematics | file | pseudocode | thread | control-statement | odd-number | conditional-statement | slice | map-function | choice | comparison | conversion | loops | append | list | basic | collection | encoding | string-iteration | filter-function | format | typeerror | complex-number | built-in-function | filter | return-statement | magic-method | counter | keyword | string-slicing | logical-operator | docstring | python-django | exception-handling | dictionary | list-mutation | memory | def | asyncio-module | tuple-unpacking | access-modifier | function | matplotlib | assert | decimal | syntax-error | string | turtle | method | comment | list-method | while-loop | global-variable ...