Python - Itertools.count()
Last Updated :
01 Mar, 2020
Python Itertools are a great way of creating complex iterators which helps in getting faster execution time and writing memory-efficient code.
Itertools
provide us with functions for creating infinite sequences and
itertools.count()
is one such function and it does exactly what it sounds like, it counts!
Note: For more information, refer to
Python Itertools
Itertools.count()
itertools.count()
are generally used with
map()
to generate consecutive data points which is useful in when working with data. It can also be used with
zip
to add sequences by passing count as parameter.
Syntax: itertools.count(start=0, step=1)
Parameters:
start: Start of the sequence (defaults to 0)
step: Difference between consecutive numbers (defaults to 1)
Returns: Returns a count object whose .__next__() method returns consecutive values.
Let us get a deep understanding of this mighty sword using some simple Python programs.
Example #1: Creating evenly spaced list of numbers
itertools.count()
can be used to generate infinite recursive sequences easily. Lets have a look
Python3 1==
# Program for creating a list of
# even and odd list of integers
# using count()
from itertools import count
# creates a count iterator object
iterator =(count(start = 0, step = 2))
# prints a odd list of integers
print("Even list:",
list(next(iterator) for _ in range(5)))
# creates a count iterator object
iterator = (count(start = 1, step = 2))
# prints a odd list of integers
print("Odd list:",
list(next(iterator) for _ in range(5)))
Output :
Even list: [0, 2, 4, 6, 8]
Odd list: [1, 3, 5, 7, 9]
In the same way, we can also generate a sequence of negative and floating-point numbers. For better accuracy of floating-point numbers use
(start + step * i for i in count())
.
Example #2: Emulating enumerate()
using itertools.count()
As mentioned earlier,
count()
can be used with
zip()
. Let's see how can we use it to mimic the functionality of
enumerate()
without even knowing the length of list beforehand!
Python3 1==
# Program to emulate enumerate()
# using count()
# list containing some strings
my_list =["Geeks", "for", "Geeks"]
# count spits out integers for
# each value in my list
for i in zip(count(start = 1,
step = 1), my_list):
# prints tuple in an enumerated
# format
print(i)
Output :
(1, 'Geeks')
(2, 'for')
(3, 'Geeks')
Note: Extra care must be taken while using
itertools.count()
as it is easy to get stuck in an infinite loop.
The following code functions the same as
while True:
thus proper termination condition must be specified.
for i in count(start=0, step=2):
print(i)
Similar Reads
Python Itertools Python's Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. For example, let's suppose there are two lists and we wa
12 min read
Python - Itertools.count() Python Itertools are a great way of creating complex iterators which helps in getting faster execution time and writing memory-efficient code. Itertools provide us with functions for creating infinite sequences and itertools.count() is one such function and it does exactly what it sounds like, it co
3 min read
Python - Itertools.cycle() Iterator is defined as object types which contains values that can be accessed or iterated using a loop. There are different iterators that come built-in with Python such as lists, sets, etc. Itertools is the Python module that contains some inbuilt functions for generating sequences using iterators
3 min read
Python - itertools.repeat() Pythonâs Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. Note: For more information, refer to Python Itertools re
2 min read
Itertools.accumulate()-Python itertools.accumulate() is an iterator that takes two arguments, an iterable (target) and an optional function. The function is applied at each iteration to accumulate the result. By default, if no function is provided, it performs addition. If the input iterable is empty, the output will also be emp
3 min read
Python - Itertools.chain() 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 easily. One such itertools function is chain().Note: For more information, refer to Python Itertools chain() function It is
4 min read
Python - Itertools.chain.from_iterable() Python's Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. Note: For more information, refer to Python Itertools Th
2 min read
Python - Itertools.compress() Pythonâs Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. Note: For more information, refer to Python Itertools Co
2 min read
Python - Itertools.dropwhile() Itertools is a Python module that provide various functions that work on iterators to produce complex iterators. It makes the code faster, memory efficient and thus we see a better performance. This module is either used by themselves or in combination to form iterator algebra. Note: For more inform
1 min read
Python - Itertools.filterfalse() In Python, Itertools is the inbuilt module that allows us to handle the iterators in an efficient way. They make iterating through the iterables like lists and strings very easily. One such itertools function is filterfalse(). Note: For more information, refer to Python Itertools filterfalse() funct
2 min read