1. islice (iterable, start, stop, step) : — this iterator selectively prints the values referenced in its iterable container passed as an argument. This iterator takes 4 arguments, an iterative container, start position, end position, and stride.
2. starmap (func., List of tuples) : — This iterator takes a function and a list of tuples as an argument and returns a value according to the function from each tuple in the list.
|
Output:
The sliced list values are: [4, 7, 10] The values acc. to function are: [1, 1, 4, 1]
3. takewhile (func, iterable) : — this iterator is the opposite of drop while (), it prints the values until the function returns false the first time.
4. tee (iterator, count) : — This iterator splits the container into multiple iterators mentioned in the argument.
|
Output:
The list values till 1st false value are : [2, 4, 6] The iterators are: [2, 4, 6, 7, 8, 10, 20] [2, 4, 6, 7, 8, 10, 20] [2, 4, 6, 7 , 8, 10, 20]
5. zip_longest (iterable1, iterable2, fillval.) : — this iterator prints the values of the iterables one at a time . If one of the iterations is printed completely, the remaining values are filled with the values assigned to fillvalue .
|
Output:
The combined values of iterables is: (’G’,’ e’) (’e’,’ k’) (’s’,’ f’) (’o’,’ r ’) (’ G’, ’e’) (’ e’, ’k’) (’ s’, ’_’)
Combinatorial iterators
1. product (iter1, iter2) : — This iterator prints the Cartesian product of the two iterable containers passed as arguments.
2. permutations (iter, group_size) : — this iterator prints all possible permutations of all elements of the iterable. The size of each permutation group is determined by the group_size argument.
|
Output:
The cartesian product of the containers is: [(’A’, ’1’), (’ A’, ’2’), (’ B’, ’1’), (’ B’, ’2’)] All the permutations of the given container is: [(’ G’, ’f’), (’ G’, ’G’), (’ f’, ’G’), (’ f’, ’G’), (’ G’, ’G’), (’ G’, ’f’)]
3. combinations (iterable, group_size) : — this iterator prints all possible combinations (no replacement) of the container passed in arguments in the specified group size in sorted order.
4. combinations_with_replacement (iterable, group_size) : — this iterator prints all possible combinations (with replacement) of the container passed in arguments in the specified group size in sorted order.
|
Output:
All the combination of container in sorted order (without replacement) is: [(’1’,’ 2 ’), (’ 1’, ’3’), (’ 1’, ’4’), (’ 2’, ’3’), (’ 2’, ’4’), (’ 3’, ’4 ’)] All the combination of container in sorted order (with replacement) is: [(’ G’, ’G’), (’ G’, ’f’), (’ G’, ’G’), (’ f’, ’f’), (’ f’, ’G’), (’ G’, ’G’)]
Infinite iterators
1. count (start, step) : — This iterator starts printing at "start" and prints indefinitely . If steps are mentioned, numbers are skipped, otherwise step is 1 by default.
Example:
iterator.count (5,2) prints - 5,7,9,11 .. .infinitely
2. loop (iterable) : — this iterator prints all values in order from the passed container. It resumes printing from the beginning again when all items are cycled .
Example:
iterator.cycle ([1,2,3,4]) prints - 1,2,3,4,1,2,3,4,1 ... infinitely
3. repeat (val, num) : — This iterator repeatedly prints the passed value an infinite number of times. If a number is mentioned, they are up to that number.
|
Output:
Printing the numbers repeatedly: [25, 25, 25, 25]
This article courtesy of Manjeet Singh . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.
Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.