Python

Python Tutorial: Itertools Module – Iterator Functions for Efficient Looping



In this Python Programming Tutorial, we will be learning about the itertools module. The itertools module is a collection of functions that allows us to work with iterators in an efficient way. Depending on your problem, this can save you a lot of memory and also a lot of work. Let’s get started…

Functions Covered in This Video:
count – 1:19
zip_longest – 6:48
cycle – 9:17
repeat – 11:09
starmap – 14:06
combinations – 15:34
permutations – 15:34
product – 19:45
chain – 21:40
islice -…

Similar Posts

39 thoughts on “Python Tutorial: Itertools Module – Iterator Functions for Efficient Looping
  1. One remark on the tee function… It does allow one to have "copies" of iterators but it's an illusion. The copies are in fact buffered iterators, so be careful with memory because the second "copy" will be using the items that were pushed into memory when you were exhausting the first "copy."

  2. I'm learning and using courses and tutorials almost all my life. And I can say with certainty – Corey is brilliant at teaching!

  3. Hi Corey,
    Great Video. Just wanted to comment that the first example that you showed with the zip and itertools.count() function is similar to the enumerate function like
    data=[100,200,300]
    my_data=list(enumerate(data))
    print(my_data)
    similar result

  4. one suggestion I have is to teach the module with some more in depth problem or even project examples. I've gone through the actual docs line by line in the past but the only time I really remember the "feeling" of how to use these tools is when I applied them in actually creating solution to a problem or even a code challenges. I don't think we learn to code by memorization but through implementation and by example.

  5. I know most of them, but it was very nice to see other examples. Very nice video! Could you do something like this with the re or with the collections library?

  6. This is an excellent video. Thank you, Corey! Sure, there are alternative (but more verbose) ways of carrying out many of these operations using "base python" But in the same way that comprehensions allow one to get away with writing less loops, it's the expressiveness of itertools that has me sold. As a pandas user, it was also interesting to pick up some new ideas about how 'groupby'/grouper objects can be used.

  7. tee function in last doesn't create true individual copies i tried to use copy1 and copy2 separately but copy 2 had no data in tuple's group maybe that got exhausted
    for k,g in c2:

    print(k)

    for i in g:

    print(i)

    print()

    for i,j in c1:

    print(i,len(list(j)))

  8. Nobody in this planet could have explained better than you. Keep up your good work. Just a small request, if you could break your python playlist in small categories it would make related videos easy to find.

  9. Hi Corey,
    Since I struggled to really get the `itertools.product` here is what I found, maybe it can help someone else :

    In the video the `product` is presented just after `combinations` and `permutations` functions. Those functions take one iterator as an argument and `n` as the length of the output tuples. The composition of the tuples are very well described in the video, but what we control here is the length via the second positional argument.

    The `product` function takes n iterators in parameter and output a sequence of n-length tuple. So we decide the length of each tuple via the number of sequences placed in argument. `repeat` does NOT dictate the number of repetition of each character inside each tuple. Instead `product(iter, repeat=i)` means that the final tuple will have a length of i and the composition of each tuple is the union of `combinations_with_replacement(iter, i)` and `permutations(inter, i)`.

    Maybe it's not a good way to clarify this function, maybe this explanation is not the most accurate (please comment if this is the case), but for me it helps me to really get how the product function works.

    Thanks Corey for the excellent work, as usual.

Leave a Reply

Your email address will not be published. Required fields are marked *