Python Iterators Overview

Apne python me baki programming languages ke comparison me kuch difference notice kiya hoga. 

jaise hum For loop ke example se samajhte hain: 

C language:
list_val=[34,56,66,34,33]
for(i=0;i<=5;i++)
{
       printf(list_val[i])
}

Ab python me yahi code dekhte hain:
list_val = [34,56,66,34,33]
for i in list_val:
    print(i)

Notice karne wali baat yah hai ki “C” language me ‘i’ ki value ko increment karke uske output ke print karaya ja raha hai aur wahi “python” me increment ka koi code nhi hai, fir bhi dono code ka output same ayega.

Ab kyuki python ke case me koi increment ka code nhi likha gaya iska matlab yah nhi hai ki python increment ki functionality use nhi kar raha hai. Python bhi iss concept ko use karta hai lekin backend me, aur yah backend ka support “iterators” provide karte hain.

Python me Iterators ka concept

Iterator ek object hai jo iteration ki process ko handle karta hai.
Ab Iteration hota kya hai? isse samajhne ke liye fir se C language ke code ko dekhte hain…

C language:
list_val=[34,56,66,34,33]
for(i=0;i<=5;i++)
{
       printf(list_val[i])
}

upar diye code me for loop me “i” ki value initialize ho rahi hai uske baad for loop condition ko check kar raha hai aur fir “i” ki value me increment bhi ho raha hai. Iske baad jab tak for loop ki condition “True” rahegi tab tak iska output print hota rahega aur “False” hone par output print hona band ho jayega. To iss “True” aur “False” ke bich jo bhi process “for loop” ke through hua wah “iteration” ka concept hai. 

Isi tarah Python me iteration ki process ko manage karne ke liye 2 functions hote hai iter() aur next() , jo iterator object ke initialize hone par apne aap active ho jate hain aur apni functionality ko handle kate hain. Python me Lists, tuples, dictionaries, strings and sets ye sabhi iterable objects jo ek container jaisa hota hai jaha se iterator iteration ki process ko handle karta hai. 

Example se samajhte hain:

country = (“Bangladesh”,“Srilanka”,“India”)
itr_val = iter(country)

print(next(itr_val))
print(next(itr_val))
print(next(itr_val))

upar diya hua code niche diye gaye code ke jaisa hai…means dono ka output same hoga 

country = (“Bangladesh”,“Srilanka”,“India”)
for i in country:
    print(i)

Python me user defined Iterators kaise banaye?

Python me class ke concept ko use karke iterators banaye ja sakte hain aur sath hi iter() aur next() function ko bhi user ke requirement ke hisab se set bhi kiya ja sakta hai.

“””Example me ek iterator hai jo number return kar raha hai jo 2 se shuru ho raha hai aur jiska output even numbers ke sequence ko print kar raha hai jaise 2,4,6,4,8,10,12 …..”””

class Number:
    def __iter__(self):
        self.x = 2
        return self

    def __next__(self):
        a = self.x
        self.x += 2
        return a

evenclass = Number()
even = iter(evenclass)

print(next(even))
print(next(even))
print(next(even))
print(next(even))
print(next(even))

Upar diye gaye code me jaise hi [ print(next(even)) ] chala waise hi even object ‘iter(evenclass)’ ko point karega jo finally “Number” class to access karega. 

First time even ki value ‘2’ hogi kyuki even = iter(evenclass) jo baad me __next__() function se process hokar a ki value ko ‘2’ assign kar dega aur wahi par x ki value ko increment karke 4 kar dega. Aur iss tarah first __next__() ki value ‘a’ ki value se assign hokar print hogi, jo ki 2 hai.