{"id":25595,"date":"2021-11-23T08:54:46","date_gmt":"2021-11-23T03:24:46","guid":{"rendered":"https:\/\/python-programs.com\/?p=25595"},"modified":"2021-11-23T08:54:46","modified_gmt":"2021-11-23T03:24:46","slug":"python-itertools-takewhile-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-itertools-takewhile-function-with-examples\/","title":{"rendered":"Python Itertools.takewhile() Function with Examples"},"content":{"rendered":"
Itertools Module:<\/strong><\/p>\n Itertools is a Python module that contains a collection of functions for dealing with iterators. They make it very simple to iterate through iterables such as lists and strings.<\/p>\n Itertools.takewhile() Function:<\/strong><\/p>\n This allows you to consider an iterable item until the specified predicate becomes false for the first time. In most cases, the iterable is a list or a string.<\/p>\n It “takes” the element from the sequence “while” the predicate is “true,” as the name implies. This function belongs to the category of “terminating iterators.” The output cannot be used directly and must be converted to another iterable form before it can be used. They are mostly converted into lists.<\/p>\n The predicate can be either a built-in or a user-defined function. Lambda functions can also be used.<\/p>\n General Implementation Format:<\/strong><\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n predicate:<\/strong> This is Required. Iterable items are evaluated by the function.<\/p>\n iterable:<\/strong> This is Required. Elements that will be checked over a predicate.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Itertools Module: Itertools is a Python module that contains a collection of functions for dealing with iterators. They make it very simple to iterate through iterables such as lists and strings. Itertools.takewhile() Function: This allows you to consider an iterable item until the specified predicate becomes false for the first time. In most cases, the …<\/p>\ndef takewhile_function(gvn_predicte, gvn_itrable):\r\nfor itr in gvn_itrable:\r\n if gvn_predicte(itr):\r\n return(itr)\r\n else:\r\n break<\/pre>\n
takewhile(predicate, iterable)<\/pre>\n
Given List = [3, 7, 6, 4, 5, 8]\r\nfunction: num % 2 != 0<\/pre>\n
[3, 7]<\/pre>\n
Given string = \"hello123all\"\r\nfunction: character.isalpha()<\/pre>\n
h\r\ne\r\nl\r\nl\r\no<\/pre>\n
Itertools.takewhile() Function with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
1)For Lists<\/h5>\n
\n
# Import takewhile() function from itertools using the import keyword.\r\nfrom itertools import takewhile\r\n\r\n# Create a function say check_oddnumbers which accepts the number as an argument and\r\n# returns True if it is an odd number\r\n\r\n\r\ndef check_oddnumbers(num):\r\n # Return if the number is odd.\r\n return(num % 2 != 0)\r\n\r\n\r\n# Give the list as static input and store it in a variable.\r\ngvn_lst = [3, 7, 6, 4, 5, 8]\r\n# Pass the above function(check_oddnumbers) and given list as the arguments to the\r\n# takewhile() function where check_oddnumbers() function returns the odd numbers\r\n# and store it in another variable.\r\nrslt = takewhile(check_oddnumbers, gvn_lst)\r\n# Convert the above result into a list using the list() function and store it in\r\n# another variable.\r\nrslt_lst = list(rslt)\r\n# Print the above result.\r\nprint(rslt_lst)\r\n<\/pre>\n
[3, 7]<\/pre>\n
2)For Strings<\/h5>\n
\n
# Import takewhile() function from itertools using the import keyword.\r\nfrom itertools import takewhile\r\n\r\n# Create a function say check_isalpha which accepts the character as an argument\r\n# and returns True if the character is an alphabet.\r\n\r\n\r\ndef check_isalpha(ch):\r\n # Return if the argument(character) is an alphabet.\r\n return(ch.isalpha())\r\n\r\n\r\n# Give the string as static input and store it in a variable.\r\ngvn_str = \"hello1234all\"\r\n# Loop using the takewhile() function and for loop by passing the arguments as\r\n# the above check_isalpha function and given string.\r\n\r\nfor itr in takewhile(check_isalpha, gvn_str):\r\n # Inside the loop, print the iterator value.\r\n print(itr)\r\n<\/pre>\n
h\r\ne\r\nl\r\nl\r\no<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Import takewhile() function from itertools using the import keyword.\r\nfrom itertools import takewhile\r\n\r\n# Create a function say check_oddnumbers which accepts the number as an argument and\r\n# returns True if it is an odd number\r\n\r\n\r\ndef check_oddnumbers(num):\r\n # Return if the number is odd.\r\n return(num % 2 != 0)\r\n\r\n\r\n# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n\r\n# Pass the above function(check_oddnumbers) and given list as the arguments to the\r\n# takewhile() function where check_oddnumbers function returns the odd numbers.\r\n# Store it in another variable.\r\nrslt = takewhile(check_oddnumbers, gvn_lst)\r\n# Convert the above result into a list using the list() function and store it in\r\n# another variable.\r\nrslt_lst = list(rslt)\r\n# Print the above result.\r\nprint(rslt_lst)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 9 7 5 2 8 3\r\n[9, 7, 5]<\/pre>\n
2)For Strings<\/h5>\n
\n
# Import takewhile() function from itertools using the import keyword.\r\nfrom itertools import takewhile\r\n\r\n# Create a function say check_isalpha which accepts the character as an argument\r\n# and returns True if the character is an alphabet.\r\n\r\n\r\ndef check_isalpha(ch):\r\n # Return if the character is an alphabet.\r\n return(ch.isalpha())\r\n\r\n\r\n# Give the string as user input using the input() function and store it in a variable.\r\ngvn_str = input(\"Enter some random string = \")\r\n# Loop using the takewhile function and for loop by passing the arguments as\r\n# the above check_isalpha function and given string.\r\n\r\nfor itr in takewhile(check_isalpha, gvn_str):\r\n # Inside the loop, print the iterator value.\r\n print(itr)\r\n<\/pre>\n
Enter some random string = abc567yiu\r\na\r\nb\r\nc<\/pre>\n","protected":false},"excerpt":{"rendered":"