![]() ![]() The chain() iteratorĪs you might have guessed, we can use the chain() itertool to combine two lists together. Let’s keep the momentum going and try another type of terminating iterator. Why don’t you try it out and let us know in the comments? If you don’t pass any parameter then it takes the addition operator by default and computes the result. But you can also use “operator.mul” if you desire. Here, we have passed the function “operator.add” as a parameter. Now, we will use the accumulate function. Since we need the data of a stock, we will import yahoo finance libraries and retrieve the data of Tesla Inc. Well we will use the accumulate function. (TSLA) and we want to see how it adds up. For example, let’s say we have the daily percentage returns of the closing price of Tesla, Inc. This iterator can be used to perform algebraic operations on the elements of a collection. Terminating iterators produce a short output and are used for fast processing of the elements in a collection. In contrast to the infinite iterators, this type of iterator does not keep going endlessly. We will now move on to the next type of iterators, which are the opposite of infinite. I will just add here that since the "if" statement is invoked after the "print" statement, 66 is printed and then the iteration stops. ![]() Just so that the function doesn’t continue endlessly, we use the break statement to stop once it goes beyond 60. In this code, 42 is the starting point and 8 is the step. Here, we will append the count function with “itertool” to give us the function “unt” iterator and pass the parameters start and step to begin counting. Infinite iteratorsĪs the name suggests, infinite iterators are created to go through the elements of a data object infinitely, unless we pass a break statement. Let’s move forward to the first type of itertools. We have also imported the “operator” module as we will be using algebraic operators along with itertools.īut yes, it is that simple to import the module in Python. ![]() You can import itertools in your Python code with the following commands You can read about them in detail in the Python Handbook. It also returns StopIteration error once all the objects have been tracked.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |