Perquisite: Iterators in Python
Python in its definition also allows some interesting and useful iterator functions for efficient looping and making execution of the code faster. There are many build-in iterators in the module “itertools“.
This module implements a number of iterator building blocks.
Some useful Iterators :
1. accumulate(iter, func) :- This iterator takes two arguments, iterable target and the function which would be followed at each iteration of value in target. If no function is passed, addition takes place by default.If the input iterable is empty, the output iterable will also be empty.
2. chain(iter1, iter2..) :- This function is used to print all the values in iterable targets one after another mentioned in its arguments.
The sum after each iteration is : [1, 5, 10, 17] The product after each iteration is : [1, 4, 20, 140] All values in mentioned chain are : [1, 4, 5, 7, 1, 6, 5, 9, 8, 10, 5, 4]
3. chain.from_iterable() :- This function is implemented similarly as chain() but the argument here is a list of lists or any other iterable container.
4. compress(iter, selector) :- This iterator selectively picks the values to print from the passed container according to the boolean list value passed as other argument. The arguments corresponding to boolean true are printed else all are skipped.
All values in mentioned chain are : [1, 4, 5, 7, 1, 6, 5, 9, 8, 10, 5, 4] The compressed values in string are : ['G', 'F', 'G']
5. dropwhile(func, seq) :- This iterator starts printing the characters only after the func. in argument returns false for the first time.
6. filterfalse(func, seq) :- As the name suggests, this iterator prints only values that return false for the passed function.
The values after condition returns false : [5, 7, 8] The values that return false to function are : [5, 7]
This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Iterator Functions in Python | Set 2 (islice(), starmap(), tee()..)
- Mathematical Functions in Python | Set 1 (Numeric Functions)
- Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions)
- Mathematical Functions in Python | Set 3 (Trigonometric and Angular Functions)
- Mathematical Functions in Python | Set 4 (Special Functions and Constants)
- Python __iter__() and __next__() | Converting an object into an iterator
- Python | Difference between iterable and iterator
- Python | range() does not return an iterator
- Python PIL | ImageSequence.Iterator()
- Iterator Method - Python Design Patterns
- Ways to increment Iterator from inside the For loop in Python
- Operator Functions in Python | Set 2
- Time Functions in Python | Set-2 (Date Manipulations)
- Time Functions in Python | Set 1 (time(), ctime(), sleep()...)
- Calendar Functions in Python | Set 1( calendar(), month(), isleap()...)
- Calendar Functions in Python | Set 2(monthrange(), prcal(), weekday()...)
- Complex Numbers in Python | Set 2 (Important Functions and Constants)
- Complex Numbers in Python | Set 3 (Trigonometric and Hyperbolic Functions)
- Array in Python | Set 1 (Introduction and Functions)
- Array in Python | Set 2 (Important Functions)