What is iterable in python example?
Python IteratorsAn iterator is an object that contains a countable number of values. Show
An iterator is an object that can be iterated upon, meaning that you can traverse through all the values. Technically, in Python, an iterator is an object which implements the iterator protocol, which consist of the methods Iterator vs IterableLists, tuples, dictionaries, and sets are all iterable objects. They are iterable containers which you can get an iterator from. All these objects have a ExampleReturn an iterator from a tuple, and print each value: mytuple = ("apple", "banana", "cherry") print(next(myit)) Try it Yourself » Even strings are iterable objects, and can return an iterator: ExampleStrings are also iterable objects, containing a sequence of characters: mystr = "banana" print(next(myit)) Try it Yourself » Looping Through an IteratorWe can also use a ExampleIterate the values of a tuple: mytuple = ("apple", "banana", "cherry") for x in mytuple: Try it Yourself » ExampleIterate the characters of a string: mystr = "banana" for x in mystr: Try it Yourself » The Create an IteratorTo create an object/class as an iterator you have to implement the methods As you have learned in the
Python Classes/Objects chapter, all classes have a function called The The ExampleCreate an iterator that returns numbers, starting with 1, and each sequence will increase by one (returning 1,2,3,4,5 etc.): class MyNumbers: def __next__(self): myclass = MyNumbers() print(next(myiter)) Try it Yourself » StopIterationThe example above would continue forever if you had enough next() statements, or if it was used in a To prevent the iteration to go on forever, we can use the In the ExampleStop after 20 iterations: class MyNumbers: def __next__(self): myclass = MyNumbers() for x in myiter: Try it Yourself » This article was published as a part of the Data Science Blogathon IntroductionPython is a beautiful Programming Language. Because of its flexibility and incredible functionalities, most people loved this language. While working with Python, I have come across a few functionalities whose usage is not commensurate to the number of complexities they simplify. Sometimes, these are also known as “Hidden Gems” in Python. These hidden things are not known by a lot of people but they become super useful when anyone wants to work in the domain of Analytics and Data Science. In this category, Python Iterators and Iterables can be fitted. Their potential is immense! Image Source: LinkOne of the most important concepts in Python is Iteration. The term Iteration not comes alone but comes up with two other terms that pertain to iteration are iterators and iterables. Also, the understanding of iterables and iterators in Python is very important. But these python concepts are a little bit tricky to understand. In this article, I will try to give a comprehensive explanation of iterables and iterators. We will understand these things starting from what they are, how they work, and also see how to use them properly. So let’s deep dive into the article and explore the world of Python Iterators and Iterables. To follow this article properly, I assume you are familiar with the basics of Python. If not, I recommend the below popular course given by Analytics Vidhya to get started with the Python Basics: Python for Data Science Table of Contents
What is Iteration?One of the most essential principles of software development is that Don’t Repeat Yourself, which is elaborated in “The Pragmatic Programmer” Book in the following way:
One specific application of the above-mentioned principle in modern programming is the use of Iteration that involves going over a list of items, on which we performed the defined operation. One of the most basic forms of iteration is a for loop. Although many other languages such as Swiftand JavaScript use three-expression for-loop, while Python programming language uses a more concise syntax of for-loop. To understand what exactly iteration means, you have to understand the following points:
To understand what exactly iterables means, you have to understand the following points:
How we can check whether an object is Iterable or not?An object is called an iterable if you can get an iterator out of it. A simpler way to determine whether an object is iterable is to check if it supports __iter__. How? Here we use the function named dir( ), which returns the list of attributes and methods supported by an object, and by seeing all attributes and methods, we can find all and select required methods from them.
Image Source: Link For Example, Python Code: What are Python Iterators?
To understand what exactly iterators mean, you have to understand the following points:
Which in-built methods does iterator have?Iterator supports in-built dunder methods such as __iter__ and __next__ Iterators can only move forward using __next__. But remember that, iterators cannot go back or cannot be reset. To learn more about the dunder methods in Python, refer to the link Let’s learn more about the __next__ function:
For Example, number_iterator = iter([1, 2, 3, 4, 5]) print(type(number_iterator)) print(next(number_iterator)) print(next(number_iterator)) print(next(number_iterator)) print(next(number_iterator)) print(next(number_iterator)) # Once the iterator is exhausted, next() function raise StopIteration. print(next(number_iterator)) Output: Specifically, in the above code, we first created an iterator called number_iterator by passing a list of numbers to the iter() method. When we checked its type with the help of the type() function of Python, we found that it was indeed an iterator, or more specifically a list_iterator. Every time we called the next() method on the number_iterator, the iterator produced an integer for us until the StopIteration exception is raised. Why should you use Iterators?In modern days, we have a lot of data in our hands, and handling this huge amount of data creates problems for everyone who wants to do some sort of analysis with that data. So, If you’ve ever struggled with handling huge amounts of data, and your machine running out of memory, then you’ll love the concept of Iterators in Python. Therefore, Rather than putting all the data in the memory in one step, it would be better if we could work with it in bits or some small chunks, dealing with only that data that is required at that moment, right? As a result, this would reduce the load on our computer memory tremendously. And this is what exactly the iterators do! Therefore, you can use Iterators to save a ton of memory, as Iterators don’t compute their items when they are generated, but only when they are called upon. Relationship between Iterators and IterablesThe first thing we have to note is that “Iterators are Iterables”, and if we use the iterator in a for loop, then it will still
work. number_iterator = iter([1, 2, 3, 4, 5]) for element in number_iterator: print(element) Output: However, the opposite is not always true. For example, iterables like strings and lists are not iterators such that they don’t have the next() method. 'Chirag'.__next__() Output: [1, 2, 3, 4, 5].__next__() Output: Given the above differences between iterables and iterators, they’re used differently in for loops. Now, as shown in the below example, we use the same iterable (i.e., a list) in two for loops without getting any errors. By contrast, the iterator can be used just once, as completing the first for loop has already made the iterator iterate all elements such that no more elements to be iterated. # Use iterables in for-loops for multiple times number_iterable = [1, 2, 3] for i in number_iterable: print(i) print('-----') for i in number_iterable: print(i) print('-----') # Use iterators in for-loops for multiple times number_iterator = iter([1, 2, 3]) for i in number_iterator: print(i) print('-----') for i in number_iterator: print(i) # nothing is printed Output: 1 2 3 ----- 1 2 3 ----- 1 2 3 ----- Difference between Iterables and IteratorsNow, in this section we will discuss the difference between Iterables and Iterators: Iterables
Iterators
Let’s understand each point described above with the help of examples: 1. Both iterables and iterators can be iterated using for loop.Let’s understand the above concept with the help of the following example: list_1 = [1,2,3,4,5] list_2 = iter(list_1) print (list_2) # Iterating through iterable(list_1) using for loop. for element in list_1: print (element,end=" ") print (" ") # Iterating through iterator(list_2) using for loop. for element in list_2: print (element,end=" ") Output: 1 2 3 4 5 1 2 3 4 5 2. Iterables supports only iter() function. But iterators supports both iter() and next() function.Let’s understand the above concept with the help of the following examples: Iterable:
Iterator:
3. Iterators are also Iterables.We can get an iterator from an iterable by calling iter() function. Similarly, we can call iter() function on the iterator itself. Then, it will return the iterator object itself. Let’s understand the above concept with the help of the following examples: list_1 = [1,2,3,4,5] # Returns an iterator list_2 = iter(list_1) print (list_2) # Calling iter() function on iterator itself. list_3 = iter(list_2) print (list_3) print (list_2 == list_3) Output: True Limits of IteratorImage Source: Link Now, let’s see the limits of the iterators:
Self Learning (Helpful) Resources
Other Blog Posts by MeYou can also check my previous blog posts. Previous Data Science Blog posts. Here is my Linkedin profile in case you want to connect with me. I’ll be happy to be connected with you. For any queries, you can mail me on Gmail. End NotesThanks for reading! I hope that you have enjoyed the article. If you like it, share it with your friends also.Something not mentioned or want to share your thoughts? Feel free to comment below And I’ll get back to you. 😉 The media shown in this article are not owned by Analytics Vidhya and are used at the Author’s discretion. What is an example of iterable object?Lists, tuples, dictionaries, and sets are all iterable objects.
What types are iterable in Python?Examples of iterables include all sequence types (such as list , str , and tuple ) and some non-sequence types like dict , file objects, and objects of any classes you define with an __iter__() method or with a __getitem__() method that implements Sequence semantics.
What is iterable used for?Iterable is a cross-channel marketing platform that powers unified customer experiences and empowers you to create, optimize and measure every interaction across the entire customer journey.
What is an iterable class in Python?In a nutshell, an Iterable in Python is an object over which you iterate its elements while an Iterator, is an object that returns an Iterable object and is used to produce the values during the iteration. In other words, The Iterable object implements __iter__() method and returns an Iterator object.
|