Header Ads Widget

Iterators In Python | Python Iterators Explained

iterators-in-python-python-iterators-explained


Today we are gonna talk about how to use and build iterators in python,

Iterators is a very important concept across all programming languages,

because it's a necessity and you will feel the need wherever you will have to loop across different elements lists arrays and etc,

Today we are going to learn about how iterators are used in python and what are the nitty gritties,

What's in it for us today

1 - What is a python nitrate?

2 - What are Iterables?                       

3 - How an iterator works?

4 - Stop iteration exception

 

1 - What Is A Python Iterator

 

Python nitrator is an object that can traverse through iterable objects in python like,

lists

Tuples

Dictionaries

sets, and returns data one element at a time,

so basically you can traverse through these layers one at a time and then you can use it as per your custom requirement,

but iterator as an object is available to you is a tool available to you,

in order to traverse through each and every element.

they have implemented within for loops, comprehensions, generators in python implicitly they are not visible up front,

but they are hidden in plain sight.

So basically when you're using loops when you're using generators comprehensions implicitly python is actually converting them in the backend in an iterator object and giving you the results.

 

2 - What are Iterables?

 

What are iterables on which you can iterate, Lists, tuples, dictionaries sets as I mentioned are all iterables or iterable objects,

other than these you can't perform any iteration if you have not created an object of list tuple or dictionary or set then of course your write variable will not be of any use and,

these objects internally possess an item method iter method which is used to get an iterator a handle to a nitrater and then you can start looping It,

or you can start accessing the elements of the iterator.

 

3 - How An Iterator Works?

 

now how iterator works python iterator implements internally these two methods, 

__iter__ () and

__next__ ()

These two methods are implicitly implemented by python for any internal implicit iterator object.

But if you have to write your own custom iterator then you need to override these methods,

the iterator object is initialized using the either method,

so basically whenever you have to create an iterator object you need to initialize it with iter and then use,

next method in order to traverse through it.

 

4 - Stop Iteration Exception

Stop iteration exception so as I mentioned when we reach the end of an iterable right,

like suppose you have a list or a set which has a length of four when you have ended the fourth element and,

you're trying to access something beyond fourth element,

which is fifth then of course you can implicitly raise a stop iteration exception,

or

python will throw it so we will see that with an example but you will get a stop iteration exception.

now so to prevent in order to handle it

or

manage it in your course we use top iteration statement to raise an error,

you can raise your customer error so you can catch this exception,

and then raise your custom error in the underscore underscore next method.

Post a Comment

0 Comments