What is the difference between iteration and iterator in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 9

What is the difference between iteration and iterator in python

I want to understand the actual difference between iteration (e.g. 👉list) and iterator (e.g. 👉generator) in python. Plz explain it little bit briefly.

26th Mar 2019, 2:42 PM
Mohd Abdul Sameer
Mohd Abdul Sameer - avatar
3 Answers
+ 9
hi Sam, thanks to Stack Overflow platform I got a hit with a good information. “Iteration is a general term for taking each item of something, one after another. Any time you use a loop, explicit or implicit, to go over a group of items, that is iteration. In Python, iterable and iterator have specific meanings. An iterable is an object that has an __iter__ method which returns an iterator, or which defines a __getitem__ method that can take sequential indexes starting from zero (and raises an IndexError when the indexes are no longer valid). So an iterable is an object that you can get an iterator from. An iterator is an object with a next (Python 2) or __next__ (Python 3) method.”
26th Mar 2019, 2:59 PM
Lothar
Lothar - avatar
+ 6
U can iterate through iterable objects like lists etc. then you do it like designed from python language. (Actually the implementation of lists in python have an iterator like @Lothar (loddar) described. If u wanna iterate in a different way than python language or iterate through ur own object --> an iterator could do it.
27th Mar 2019, 4:32 PM
Oma Falk
Oma Falk - avatar
+ 1
These answers are a bit technical. Here's a simple one. To iterate is to move through the items in a collection, one at a time. Certain languages provide one or more collections , called iterators, which can be iterated on. In Python , a list is an iterator . So is a string . So, you can iterate through a string by inspecting each letter in it , one at a time. You may take some action for each one , like changing some to uppercase or counting the vowels . Similar for lists. In Python, there is a specific way to iterate: for letter in myString: or for animal in animalList:
22nd Apr 2019, 11:29 PM
Mark Matchen
Mark Matchen - avatar