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
Author
-
Naveen Pandey has more than 2 years of experience in data science and machine learning. He is an experienced Machine Learning Engineer with a strong background in data analysis, natural language processing, and machine learning. Holding a Bachelor of Science in Information Technology from Sikkim Manipal University, he excels in leveraging cutting-edge technologies such as Large Language Models (LLMs), TensorFlow, PyTorch, and Hugging Face to develop innovative solutions.
View all posts