Difference between iterables and iterators in Python
- Naveen
- 0
Iterables
- iterables is an object over which we can iterate using loops.
- In other words, any object over which we can perform iteration and access it’s elements one by one is known as iterable.
- objects such as lists, tuples, sets, strings, dictionaries are called iterables.
- iterables support iter() function.
Iterators
- iterator is generated when the iterable object is passed to iter() function.
- iterator is used to iterate over an iterable object using next() function.
- next() function always returns the next elements from the iterator.
- if there is no next element in the iterator then next() function raises Stopiteration exception.
Iterators can be iterable because both iterator and iterable can be iterated using for loop but iterable objects can not be iterator because next() method is not supported by iterable objects.
Popular Posts
Spread the knowledge