ADVANCED LIST PROCESSING — ITERATOR AND GENERATOR TRB CSI › Python Programming › Advanced list processing (iterator and generator)

ADVANCED LIST PROCESSING — ITERATOR AND GENERATOR

Advanced List Processing என்பது list அல்லது மற்ற iterable collections-ல் உள்ள elements-ஐ memory-efficient மற்றும் controlled way-ல் process செய்வது. இந்த topic-ல் இரண்டு முக்கிய concepts:

Premium — locked Advanced 25 min 198 cards 31 programs 13 MCQs v4
This is a premium lesson. You can see the shape of every card — unlock to read them. Unlock

ADVANCED LIST PROCESSING - IT……

Premium This card is part of the premium lesson. Unlock
Definition

Advanced List Processing என்பது list அல்லது மற்ற iterable collections-ல் ……

Premium This card is part of the premium lesson. Unlock
Definition

1. Iterator 2. Generator…

Premium This card is part of the premium lesson. Unlock
Definition

Simple idea: List ↓ One item at a time process செய்ய ↓ Itera……

Premium This card is part of the premium lesson. Unlock
Simple Explanation

Suppose: numbers = [10, 20, 30, 40] Normal list already contains all values: 10 20 30 40 ஒரு for loop: for x ……

Premium This card is part of the premium lesson. Unlock
Flow

Iterable ↓ iter() ↓ Iterator ↓ next() ↓ One value ↓ next() ↓ Next value ↓ ... ↓ StopIteration Generat……

Premium This card is part of the premium lesson. Unlock
Definition

Iterator என்பது collection-ல் உள்ள elements-ஐ one by one ……

Premium This card is part of the premium lesson. Unlock
Explanation
Example

numbers = [10, 20, 30] இந்த list i……

Premium This card is part of the premium lesson. Unlock
Comparison
Iterable vs iterator

இந்த இரண்டு terms மிகவும் important. Iterable L……

Premium This card is part of the premium lesson. Unlock
Explanation
Example

numbers = [10, 20, 30] numbers is iterable.……

Premium This card is part of the premium lesson. Unlock
Explanation

it is iterator. Memory: Iterable ……

Premium This card is part of the premium lesson. Unlock
Explanation

Next value get: next(iterator……

Premium This card is part of the premium lesson. Unlock
Explanation

numbers = [10, 20, 30] List created. it = iter(numbers) Python list-க்கான iterator object create செய்கிறது……

Premium This card is part of the premium lesson. Unlock
Dry Run
numbers = [10, 20, 30]
Explanation
Stopiteration

Suppose: numbers = [10, 20] it = iter(numbers) print(next(it)) print(……

Premium This card is part of the premium lesson. Unlock
Important

StopIteration means: Iterator has no more values. It i……

Premium This card is part of the premium lesson. Unlock
Explanation
For loop and iterator

When writing: for x in [10, 20, 30]: print(x) Python conceptually does something similar to: it =……

Premium This card is part of the premium lesson. Unlock
Concept
Important concept

for loop = automatic iterator handling You ……

Premium This card is part of the premium lesson. Unlock
Explanation
Iterator with string

String is iterable. text = "ABC" it =……

Premium This card is part of the premium lesson. Unlock
Explanation
Iterator with tuple

data = (100, 200, 300) it = i……

Premium This card is part of the premium lesson. Unlock
Explanation
Iterator with dictionary

Dictionary iteration gives keys by default. student = { "name": "Arun", "ma……

Premium This card is part of the premium lesson. Unlock
Explanation
Iterator is stateful

Iterator remembers where it s……

Premium This card is part of the premium lesson. Unlock
Program
If later

It does not restart from 1. T……

Premium This card is part of the premium lesson. Unlock
Explanation
Iterator exhaustion

Once an iterator is fully consumed, it cannot automat……

Premium This card is part of the premium lesson. Unlock

Why second empty list? Iterat……

Premium This card is part of the premium lesson. Unlock
Explanation
New iterator required

To start again: it = iter(num……

Premium This card is part of the premium lesson. Unlock
Explanation
Iterator protocol

Python iterator protocol mainly requires: __iter__() ……

Premium This card is part of the premium lesson. Unlock
Explanation
Custom iterator

We can create our own iterator class. class Numbers: def __in……

Premium This card is part of the premium lesson. Unlock
Explanation
Custom iterator line explanation

self.current = 1 Starting value. def __iter__(self): return se……

Premium This card is part of the premium lesson. Unlock
Definition

Generator என்பது values-ஐ ஒரே நேரத்தில் எல்லாம் create/store செய்யாமல், தேவைப்படும் போது……

Premium This card is part of the premium lesson. Unlock
Simple Explanation

Normal function: def test(): return 10 Function returns value and ends. Generator function: def test(): yiel……

Premium This card is part of the premium lesson. Unlock
Syntax
Usage
Explanation

next(g) or:…

Premium This card is part of the premium lesson. Unlock
Important

Calling: g = numbers() does not execute the complete functi……

Premium This card is part of the premium lesson. Unlock
Dry Run
Generator dry run
Explanation
Yield

yield is the heart of generat……

Premium This card is part of the premium lesson. Unlock
Important

return → sends value → function ends yield……

Premium This card is part of the premium lesson. Unlock
Comparison
Return vs yield

Normal function: def test(): ……

Premium This card is part of the premium lesson. Unlock

Because function ends after f……

Premium This card is part of the premium lesson. Unlock

Output through loop: 10 20…

Premium This card is part of the premium lesson. Unlock
Explanation
Generator with for loop

def numbers(): yield 1 yield ……

Premium This card is part of the premium lesson. Unlock

for automatically calls gener……

Premium This card is part of the premium lesson. Unlock
Explanation
Generator using loop

Better real example: def generate_numbers(n): ……

Premium This card is part of the premium lesson. Unlock
Explanation

def generate_numbers(n): Generator function. for i ……

Premium This card is part of the premium lesson. Unlock
Important

Generator does not create full list: [……

Premium This card is part of the premium lesson. Unlock
Explanation
Generator for squares

Normal list processing: squares = []……

Premium This card is part of the premium lesson. Unlock

All values stored in list. Ge……

Premium This card is part of the premium lesson. Unlock

Values produced one-by-one.…

Premium This card is part of the premium lesson. Unlock
Comparison
List vs generator memory

Suppose: numbers = [x*x for x in range(1000000)] This creates and stores all one million values. Memory usage……

Premium This card is part of the premium lesson. Unlock
Explanation
Lazy evaluation

Lazy evaluation means: Do not……

Premium This card is part of the premium lesson. Unlock
Program
Example
Explanation

At this stage, all squares are not necessarily mat……

Premium This card is part of the premium lesson. Unlock
Explanation
Generator expression

Like list comprehension: [x*x for x in range(5)] Generator expres……

Premium This card is part of the premium lesson. Unlock
Program
Program generator expression
Comparison
List comprehension vs generator expression

List comprehension: result = ……

Premium This card is part of the premium lesson. Unlock
Output
Result
Program
Type
Program
Generator expression
Program
Type

Output similar to:…

Premium This card is part of the premium lesson. Unlock
Explanation
Generator exhaustion

Generators are iterators. Once consumed, they……

Premium This card is part of the premium lesson. Unlock

Because first list(g) consume……

Premium This card is part of the premium lesson. Unlock
Explanation
Generator state

Generator remembers local var……

Premium This card is part of the premium lesson. Unlock
Program
Example
Explanation

yield x x += 1 yield x x += 1……

Premium This card is part of the premium lesson. Unlock

x value is remembered between……

Premium This card is part of the premium lesson. Unlock
Explanation
Generator with condition

def even_numbers(n): for i in range(1, n ……

Premium This card is part of the premium lesson. Unlock
Explanation
Generator for list processing

Suppose list: numbers = [1, 2, 3, 4, 5] We need only squares of ev……

Premium This card is part of the premium lesson. Unlock

Processing: 1 → odd → ignore 2 → e……

Premium This card is part of the premium lesson. Unlock
Explanation
Chained processing

Generator expressions useful for data pipelines. numbers = range(1,……

Premium This card is part of the premium lesson. Unlock

Flow: 1..10 ↓ Filter even ↓ 2,4,6,8,1……

Premium This card is part of the premium lesson. Unlock
Comparison
Iterator vs generator

Iterator General object that produces one item at a time. Can be created using: iter() or……

Premium This card is part of the premium lesson. Unlock
Remember

Every generator is an iterator.……

Premium This card is part of the premium lesson. Unlock
Program
Example
Explanation

This is iterator. But it is n……

Premium This card is part of the premium lesson. Unlock
Explanation
Iterable iterator generator relation

Iterable | | iter() ↓ Iterator | | next() ↓ Values Generator: Generator ↓ ……

Premium This card is part of the premium lesson. Unlock
Important
Important difference list and iterator

List: numbers = [10, 20, 30] Can be traversed repeatedly: for x in numbers: print(x) for x in n……

Premium This card is part of the premium lesson. Unlock
Comparison
List vs generator
Explanation
Generator no indexing

List: numbers = [10, 20, 30] ……

Premium This card is part of the premium lesson. Unlock

Generator: g = (x for x in [1……

Premium This card is part of the premium lesson. Unlock

Why? Generator values are pro……

Premium This card is part of the premium lesson. Unlock
Explanation
Generator no len

This: g = (x for x in range(5)) print(len(g)) causes error. Generators do n……

Premium This card is part of the premium lesson. Unlock
Explanation
Next with default

next() can accept a default v……

Premium This card is part of the premium lesson. Unlock
Program
Example

This avoids visible StopItera……

Premium This card is part of the premium lesson. Unlock
Explanation
Why use iterators

Iterators useful when: Large collection process செய்ய One item at a time traverse செய்ய Memory usage reduce ……

Premium This card is part of the premium lesson. Unlock
Explanation
Why use generators

Generators useful when: Large number of values Infinit……

Premium This card is part of the premium lesson. Unlock
Explanation
Infinite generator

Generators can represent infi……

Premium This card is part of the premium lesson. Unlock
Program
Example
Program
Usage carefully

Generator doesn't need to create ……

Premium This card is part of the premium lesson. Unlock
Explanation
Fibonacci generator

Important advanced example: def fibonacci(n): a = 0……

Premium This card is part of the premium lesson. Unlock
Dry Run
Fibonacci dry run
Explanation
Memory efficiency

Consider: numbers = [x for x in range(10000000)] Python must maintain millions of integer references in a lis……

Premium This card is part of the premium lesson. Unlock
Comparison
Eager vs lazy

Eager Evaluation [x*x for x in range(5)] Immediately calculates: ……

Premium This card is part of the premium lesson. Unlock
Explanation
Example

next(g) one item at a time.…

Premium This card is part of the premium lesson. Unlock
Common Mistake
Common error 1

Calling next() directly on li……

Premium This card is part of the premium lesson. Unlock
Common Mistake
Error

TypeError Because list is iterable but no……

Premium This card is part of the premium lesson. Unlock
Common Mistake
Common error 2

Confusing return and yield. Wrong generator understanding: def numb……

Premium This card is part of the premium lesson. Unlock
Common Mistake
Common error 3

Trying to reuse exhausted generator……

Premium This card is part of the premium lesson. Unlock

Generator already consumed.…

Premium This card is part of the premium lesson. Unlock
Common Mistake
Common error 4

Trying indexing: g = (x for x……

Premium This card is part of the premium lesson. Unlock
Common Mistake
Error.

Generators are sequential, no……

Premium This card is part of the premium lesson. Unlock
Common Mistake
Common error 5

Thinking generator expression is tuple: x = (n*n for n……

Premium This card is part of the premium lesson. Unlock
Important

Iterator facts…

Premium This card is part of the premium lesson. Unlock
Important

1. Iterator gives one item at a time. 2. Created using iter(). 3. next() retriev……

Premium This card is part of the premium lesson. Unlock
Important

Generator facts…

Premium This card is part of the premium lesson. Unlock
Important

1. Generator is a special iterator. 2. Uses yield. 3. Produces values lazily. 4. M……

Premium This card is part of the premium lesson. Unlock
Memory Trick

For iterator: ITERATOR = ITER + NEXT iter() ↓ ……

Premium This card is part of the premium lesson. Unlock
Remember

return → END yield → PAUSE Most important……

Premium This card is part of the premium lesson. Unlock
MCQ

What does iter() do?…

Premium This card is part of the premium lesson. Unlock
TRB Point
Trb point 2

What does next() do? It retri……

Premium This card is part of the premium lesson. Unlock
TRB Point
Trb point 3

What happens after all iterat……

Premium This card is part of the premium lesson. Unlock
TRB Point
Trb point 4

Which keyword creates a gener……

Premium This card is part of the premium lesson. Unlock
MCQ

Which is a generator expressi……

Premium This card is part of the premium lesson. Unlock
TRB Point
Trb point 6

x = [1, 2, 3] y = iter(x) pri……

Premium This card is part of the premium lesson. Unlock
TRB Point
Trb point 7

What is output? g = (x for x ……

Premium This card is part of the premium lesson. Unlock
TRB Point
Trb point 8

What is the fundamental difference between return and yi……

Premium This card is part of the premium lesson. Unlock
MCQ

Every generator is:…

Premium This card is part of the premium lesson. Unlock
TRB Point
Trb point 10

Every iterator is a generator. False Correct: Ever……

Premium This card is part of the premium lesson. Unlock
Interview Point
Interview point 1

Why are generators memory efficient? Because generator does not store the……

Premium This card is part of the premium lesson. Unlock
Interview Point
Interview point 2

Difference between iterable a……

Premium This card is part of the premium lesson. Unlock
Explanation
Example

[1, 2, 3] Iterator: Can directl……

Premium This card is part of the premium lesson. Unlock
Interview Point
Interview point 3

Why can a list be reused but a generator often cannot? A list stores values. ……

Premium This card is part of the premium lesson. Unlock
Interview Point
Interview point 4

Is range() a generator? No. range() is an iterable se……

Premium This card is part of the premium lesson. Unlock
Program
Example
MCQ

Which function converts an it……

Premium This card is part of the premium lesson. Unlock
MCQ

Which function retrieves the ……

Premium This card is part of the premium lesson. Unlock
MCQ

When an iterator is exhausted……

Premium This card is part of the premium lesson. Unlock
MCQ

Which keyword is used in gene……

Premium This card is part of the premium lesson. Unlock
MCQ

What is the output? a = [10, ……

Premium This card is part of the premium lesson. Unlock
MCQ

Which uses lazy evaluation?…

Premium This card is part of the premium lesson. Unlock
MCQ

What is the type? x = (n*n fo……

Premium This card is part of the premium lesson. Unlock
MCQ

Which statement is correct?…

Premium This card is part of the premium lesson. Unlock
MCQ

yield does what?…

Premium This card is part of the premium lesson. Unlock
MCQ

Generator values are normally……

Premium This card is part of the premium lesson. Unlock
Practice
Practice 1

Predict: a = [5, 10, 15] it = i……

Premium This card is part of the premium lesson. Unlock
Practice
Practice 2

Predict: def test(): yield 10……

Premium This card is part of the premium lesson. Unlock
Practice
Practice 3

Predict: g = (x+1 for x in ra……

Premium This card is part of the premium lesson. Unlock
Practice
Practice 4

Predict: g = (x for x in range(3)……

Premium This card is part of the premium lesson. Unlock
Practice
Practice 5

Predict: def even(): for x in range(1, 7……

Premium This card is part of the premium lesson. Unlock
Summary

Iterator An iterator is an object that provides elements one at a time. numbers = [10, 20, 30] it = iter(numb……

Premium This card is part of the premium lesson. Unlock
Explanation
Short description

Iterator = Collection-ல் இருந்து values-ஐ one-by-one access செய்யும் object. Generator = Values-ஐ memory-ல் எ……

Premium This card is part of the premium lesson. Unlock
Text size
17px
Theme
Contents
Print / PDF
Program