Amazing itertools functions — PyTip09

Amazing itertools functions — PyTip09

The itertools module in Python is a standard library that provides functions for creating and working with iterators. Iterators are objects that can be iterated upon, and they represent a stream of data. The itertools module provides a range of functions for performing various operations on iterators, such as filtering, grouping, and combining. This can save time and effort by allowing you to perform complex operations with just a few lines of code. Overall, the itertools module is a useful tool for working with iterators and sequences of data in Python, and it can help you write more efficient, concise, and functional code.

In this tutorial you will learn some of the most useful fuctions provided by itertool module in Python. To use itertools create a Python file or Open the REPL and type the following.


>>> import itertools        

The?itertools?module is now imported and now you can use any function using?itertools.func_name?syntax.

#1 — count

The?count?function in?itertools?module is used to provide a never ending generator which you can use anywhere in the program, the best thing about generators is that they remember their state. The?count()?function takes two arguments — start and step.


>>> import itertools as it
>>> infinite_gen = it.count(43, 89)
>>> for i in range(5):
        print(next(infinite_gen))        

The above code produces following output:


43
132
221
310
399        

In the above code you can see that?infinite_gen?returned five values with each value larger than the previous one by 89. The best thing about this is that you can use this same generator anywhere in the program and it will remember its state as 399.

#2 — cycle

The?cycle?function is pretty simple — its sole purpose is to repeat a iterator passed to it over and over again. It is also a generator so it also remembers its state.

Below is the example of how to use this function:


>>> import itertools as it
>>> rep = it.cycle(['repeat', 'me', 'again'])
>>> for i in range(5):
...     print(next(rep))
...
repeat
me
again
repeat
me        

#3 — chain

The?chain()?function is used to make two or more iterables in a single sequence. This function does not modify the original iterators rather it just create a new one using the iterators passed in the argument.


>>> import itertools as it
>>> combined_list = it.chain('ABC','DEF')
>>> list(combined_list)
['A', 'B', 'C', 'D', 'E', 'F']        

In the above output you can see that?combined_list?returns the elemenst of both the iterators passed to it.

#4 — tee

The?tee()?function in python is used to make an independent copies of the original iterator instantly.

This function take two arguments — iterable and the number of times is need to be copied.

Below is the example of how to use this:


>>> import itertools as it
>>> a1, a2, a3 = it.tee([1,2,3,4], 3)
>>> for i in a1: print(i)
...
1
2
3
4
>>> for i in a2: print(i)
...
1
2
3
4        

In the above program you created three copies of original iterator in?a1, a2, a3.

In conclusion, the itertools module is a powerful tool that provides a range of functions for working with iterators and sequences of data in Python. Whether you need to generate infinite sequences of data, repeat elements or iterables, or filter, group, and combine data, the itertools module has you covered. By using the amazing features of the itertools module, you can write more efficient, concise, and functional code in Python.

In the upcoming blogs I intend to share my learning on more Advanced stuff about DevOps, Python and Django. If it interests you then consider?following ?me.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了