{"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

repeat(value, number)<\/pre>\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

Given value = 60\r\nGiven number = 5<\/pre>\n

Output:<\/strong><\/p>\n

The list of given value { 60 } 5 times =  [60, 60, 60, 60, 60]<\/pre>\n

Example2:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given string = \"hello\"\r\nGiven number = 3<\/pre>\n

Output:<\/strong><\/p>\n

The list of given string { hello } 3 times =  ['hello', 'hello', 'hello']<\/pre>\n

Itertools.repeat() Function with Examples in Python<\/h2>\n