The itertools is a module in Python having a collection of functions that are used for handling iterators. They make iterating through the iterables like lists and strings very easy. One such itertools function is takewhile()
.
Note: For more information, refer to Python Itertools
takewhile()
This allows considering an item from the iterable until the specified predicate becomes false for the first time. The iterable is a list or string in most of the cases. As the name suggests it “take” the element from the sequence “while” the predicate is “true”. This function come under the category “terminating iterators”. The output cannot be used directly and has to be converted to another iterable form. Mostly they are converted into lists.
Syntax:
takewhile(predicate, iterable)
The predicate
is either a built-in function or a user-defined function. It can be even lambda functions.
The general implementation of this function using simple if-else is given below.
def takewhile(predicate, iterable): for i in iterable: if predicate(i): return(i) else: break
The function takewhile()
takes a predicate and an iterable as arguments. The iterable is iterated to check each of its elements. If the elements on the specified predicate, evaluates to true, it is returned. Otherwise, the loop is terminated.
Example 1: Lists and takewhile()
Consider a list of integers. We need only the even numbers in the output. Look at the code below to see what happens if we use takewhile()
.
from itertools import takewhile # function to check whether # number is even def even_nos(x): return (x % 2 = = 0 ) # iterable (list) li = [ 0 , 2 , 4 , 8 , 22 , 34 , 6 , 67 ] # output list new_li = list (takewhile(even_nos, li)) print (new_li) |
[0, 2, 4, 8, 22, 34, 6]
Example 2: Strings and takewhile()
Consider an alpha-numerical String. Now we need to take the elements as long as they are digits.
from itertools import takewhile # function to test the elements def test_func(x): print ( "Testing:" , x) return (x.isdigit()) # using takewhile with for-loop for i in takewhile(test_func, "11234erdg456" ): print ( "Output :" , i) print () |
Output:
Testing: 1 Output : 1 Testing: 1 Output : 1 Testing: 2 Output : 2 Testing: 3 Output : 3 Testing: 4 Output : 4 Testing: e
The iterable can be directly passed also. It is not mandatory that they should be assigned to some variable before passing them to takewhile()
function.
Example 3: lambda functions in takewhile()
Consider the elements of the input String until a ‘s’ is encountered.
from itertools import takewhile # input string st = "Lazyroar" # consider elements until # 's' is encountered li = list (takewhile( lambda x:x ! = 's' , st)) print (li) |
Output:
['G', 'e', 'e', 'k']
Example 4:
Make a list of alphabets in random order and consider the elements until you encounter ‘e’ or ‘i’ or ‘u’.
import random from itertools import takewhile # generating alphabets in random order li = random.sample( range ( 97 , 123 ), 26 ) li = list ( map ( chr , li)) print ( "The original list list is :" ) print (li) # consider the element until # 'e' or 'i' or 'o' is encountered new_li = list (takewhile( lambda x:x not in [ 'e' , 'i' , 'o' ], li)) print ( "\nThe new list is :" ) print (new_li) |
The original list list is :
[‘v’, ‘u’, ‘k’, ‘j’, ‘r’, ‘q’, ‘n’, ‘y’, ‘a’, ‘x’, ‘i’, ‘p’, ‘e’, ‘w’, ‘b’, ‘t’, ‘s’, ‘l’, ‘z’, ‘m’, ‘f’, ‘c’, ‘g’, ‘d’, ‘o’, ‘h’]The new list is :
[‘v’, ‘u’, ‘k’, ‘j’, ‘r’, ‘q’, ‘n’, ‘y’, ‘a’, ‘x’]