{"id":25600,"date":"2021-11-23T08:57:21","date_gmt":"2021-11-23T03:27:21","guid":{"rendered":"https:\/\/python-programs.com\/?p=25600"},"modified":"2021-11-23T08:57:21","modified_gmt":"2021-11-23T03:27:21","slug":"python-itertools-repeat-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-itertools-repeat-function-with-examples\/","title":{"rendered":"Python Itertools.repeat() 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.repeat() Function:<\/strong><\/p>\n Itertools.repeat() belongs to the class of infinite iterators. In repeat(), we provide the data as well as the number of times the data will be repeated. If we don’t specify a number, it will loop indefinitely. The memory space is not created for each variable in repeat(). Rather, it creates only one variable and then repeats it.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n value:<\/strong> It is the value that will be printed.<\/p>\n number:<\/strong> If the optional keyword number is specified, it prints the passed value number times; otherwise, it prints the passed value infinite times.<\/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.repeat() Function: Itertools.repeat() belongs to the class of infinite iterators. In repeat(), we provide the data as well as the number of times the …<\/p>\nrepeat(value, number)<\/pre>\n
Given value = 60\r\nGiven number = 5<\/pre>\n
The list of given value { 60 } 5 times = [60, 60, 60, 60, 60]<\/pre>\n
Given string = \"hello\"\r\nGiven number = 3<\/pre>\n
The list of given string { hello } 3 times = ['hello', 'hello', 'hello']<\/pre>\n
Itertools.repeat() Function with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Import itertools module using the import keyword.\r\nimport itertools\r\n# Give the value as static input and store it in a variable.\r\ngvn_valu = 60\r\n# Give the number as static input and store it in another variable.\r\ngvn_numb = 5\r\n# Pass the given value and number as the arguments to the itertools.repeat()\r\n# function that gets the given value, given number of times\r\n# and store it in a variable.\r\nrslt = itertools.repeat(gvn_valu, gvn_numb)\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(\"The list of given value {\", gvn_valu, \"}\",\r\n gvn_numb, \"times = \", rslt_lst)\r\n<\/pre>\n
The list of given value { 60 } 5 times = [60, 60, 60, 60, 60]<\/pre>\n
For Strings<\/h5>\n
\n
# Import itertools module using the import keyword.\r\nimport itertools\r\n# Give the string as static input and store it in a variable.\r\ngvn_str = \"hello\"\r\n# Give the number as static input and store it in another variable.\r\ngvn_numb = 3\r\n# Pass the given string and number as the arguments to the itertools.repeat()\r\n# function that gets the given string, given number of times\r\n# and store it in a variable.\r\nrslt = itertools.repeat(gvn_str, gvn_numb)\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(\"The list of given string {\", gvn_str, \"}\",\r\n gvn_numb, \"times = \", rslt_lst)\r\n<\/pre>\n
The list of given string { hello } 3 times = ['hello', 'hello', 'hello']<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Import itertools module using the import keyword.\r\nimport itertools\r\n# Give the value as user input using the int(input()) function and store it in a variable.\r\ngvn_valu = int(input(\"Enter some random number = \"))\r\n# Give the number as user input using the int(input()) function and store it in another variable.\r\ngvn_numb = int(input(\"Enter some random number = \"))\r\n# Pass the given value and number as the arguments to the itertools.repeat()\r\n# function that gets the given value, given number of times\r\n# and store it in a variable.\r\nrslt = itertools.repeat(gvn_valu, gvn_numb)\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(\"The list of given value {\", gvn_valu, \"}\",\r\n gvn_numb, \"times = \", rslt_lst)\r\n<\/pre>\n
Enter some random number = 75\r\nEnter some random number = 7\r\nThe list of given value { 75 } 7 times = [75, 75, 75, 75, 75, 75, 75]<\/pre>\n
For Strings<\/h5>\n
\n
# Import itertools module using the import keyword.\r\nimport itertools\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# Give the number as user input using the int(input()) function and store it in another variable.\r\ngvn_numb = int(input(\"Enter some random number = \"))\r\n# Pass the given string and number as the arguments to the itertools.repeat()\r\n# function that gets the given string, given number of times\r\n# and store it in a variable.\r\nrslt = itertools.repeat(gvn_str, gvn_numb)\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(\"The list of given string {\", gvn_str, \"}\",\r\n gvn_numb, \"times = \", rslt_lst)\r\n<\/pre>\n
Enter some random string = btechgeeks\r\nEnter some random number = 2\r\nThe list of given string { btechgeeks } 2 times = ['btechgeeks', 'btechgeeks']<\/pre>\n","protected":false},"excerpt":{"rendered":"