+ 4

Dont understand Iterators Python

You apply __iter__() to an object that contains many values and it becomes an iterator. But in alot of guides, they say that containers such as list,tuples and dictionaries are iterable. Then why do I even need to make a list an iterator if it was iterable in the first place???????? My thoughts are that list,tuples and dictionaries are objects that contain many values and in a sequence. And thats it...contains many elements. Then I thought that the __iter__() when used on a list, makes that container an object that you CAN actually iterate through i.e perform a set of instructions on each element. Then using __next__(), you perform your set of instructions on an element within the container, depending on the sequence and retrieve just one result. Am I missing something that I need to learn before this or what??? P.s I have looked alot on this on places such as: https://stackoverflow.com/questions/25653996/what-is-the-difference-between-list-and-iterator-in-JUMP_LINK__&&__python__&&__JUMP_LINK https://www.geeksforgeeks.org/python-difference-iterable-iterator/ https://www.datacamp.com/community/tutorials/python-iterator-tutorial And of course Youtube.com If anyone can simplify what it is and why we need to make a list into an iterator to loop through, in like a really dumb way or an anology it would really help me alot.

10th Aug 2019, 11:08 AM
J3fro C
J3fro C - avatar
6 odpowiedzi
+ 5
Iterators are python's way to implement lazy sequences, which is an important concept in functional programming. It allows the program to process a single value at a time, even if the sequence has huge number of elements or even infinite. There is no need to load all the elements into memory if we only work with one at a time. You should also check out generator functions, maybe this example will help to realize how they can be useful: https://code.sololearn.com/cYlH5XVg4s75/?ref=app https://code.sololearn.com/cLC0Ya2wj7Aq/?ref=app The itertools library has a number of functions which operate with iterators hence they are python's core toolbox for lazy programming. Something I made recently: https://code.sololearn.com/c0srq254jCmn/?ref=app
10th Aug 2019, 12:09 PM
Tibor Santa
Tibor Santa - avatar
+ 3
J3fro C You don't absolutely NEED to use iter() on the regular python containers. You can simply iterate through each element of a list, set or dictionary with a for loop too. If you create an iterator from them, then you have additional control to advance to the next item when needed. Following post shows a few ways to iterate through a set: https://www.geeksforgeeks.org/iterate-over-a-set-in-python/
10th Aug 2019, 3:11 PM
Tibor Santa
Tibor Santa - avatar
+ 2
I think after going through stackoverflow, I want to confirm my further understanding on iterators as a whole: So 'iterables' are just the containers/objects i.e list/dictionaries. They CAN be iterated BUT ONLY if you can produce an iterator for it. By using iter() onto a list, you return/produce an iterator. This iterator is an object that kind of points to (?) or knows which is the next element (I only say next ONLY when next() is applied to it) within the container. So the next time 'next()' is applied, iterator object to point towards the next element (and from then remembers it's position state), which then 'next()' retrieves that element. i.e the iterator object doesn't initially have a value stored nor does it remember a state initially. It only iterates a value and remembers a state ONLY when 'next()' is applied to it. Furthermore, in a 'for' loop e.g: for x in thingylist The thingylist in this case is the iterator object for a list called 'thingylist' and implements the next() method. The next() method access an element depending on the iterator's state and assigns this element as the value of variable x? But also tells the iterator to point/move onto the next element.
10th Aug 2019, 7:43 PM
J3fro C
J3fro C - avatar
+ 1
But it still doesn't explain WHY I need to return an iterator from an object like a list,dictionary etc in order to perform some code on each element inside it. Like WHY do I have to use __iter__() onto a list, when in several websites and videos, they say objects like list,dictionary and etc are 'iterable' meaning you CAN iterate/traverse through it. In my initial post, I wanted to confirm my understanding of WHY i.e, Objects such as list,dictionaries and etc are just containers of many values...but I am guessing you CANNOT iterate through each element in such object unless you make your list into an iterator????
10th Aug 2019, 1:47 PM
J3fro C
J3fro C - avatar
0
__iter__() is a meta function that is indirectly called if you treat an iterable like an iterable. It is used to declare how own classes should be used as iterables. 'in' is a keyword that implicitly calls __iter__() However, you can of course use __iter__() in your program flow, it just is unnecessary.
11th Aug 2019, 1:31 PM
Loeschzwerg