Level 5 : Filtering numbers in an iterator.


class FilterIterator:
    def __init__(self, data, condition):
        self.data = data
        self.condition = condition
        self.index = 0

    def __iter__(self):
        return self

    def __next__(self):
        while self.index < len(self.data):
            value = self.data[self.index]
            self.index += 1
            if self.condition(value):
                return value
        raise StopIteration

it = iter(FilterIterator([1, 2, 3, 4, 5, 6], lambda x: x % 2 == 0))
print(list(it))

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