Python Tutorial: Itertools Module – Iterator Functions for Efficient Looping

///Python Tutorial: Itertools Module – Iterator Functions for Efficient Looping

Python Tutorial: Itertools Module – Iterator Functions for Efficient Looping

FavoriteLoadingAdd to favorites

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 – 23:37
compress – 28:50
filterfalse – 31:49
dropwhile – 32:24
takewhile – 32:24
accumulate – 34:54
groupby – 37:04
tee – 43:28

The code from this video can be found at:

Iterators Tutorial:

Sorting Tutorial:

✅ Support My Channel Through Patreon:

✅ Become a Channel Member:

✅ One-Time Contribution Through PayPal:

✅ Cryptocurrency Donations:
Bitcoin Wallet – 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet – 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet – MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot

✅ Corey’s Public Amazon Wishlist

✅ Equipment I Use and Books I Recommend:

▶️ You Can Find Me On:
My Website –
My Second Channel –
Facebook –
Twitter –
Instagram –

#Python #Itertools

source

By |2019-09-10T22:45:11+00:00September 10th, 2019|Python Video Tutorials|39 Comments

39 Comments

  1. Justin Williams September 10, 2019 at 10:45 pm - Reply

    I see no module called just simply "itertools". They are all similar but none exactly like that. Which is the one we should use now?

  2. Dennis Asamoah September 10, 2019 at 10:45 pm - Reply

    great content

  3. AHMED SALIH Fadhil September 10, 2019 at 10:45 pm - Reply

    Hi Corey, thanks for the valuable resources you are providing. Could you please make a video on collections and their use in dictionary. Thank you

  4. Dhruv Singh September 10, 2019 at 10:45 pm - Reply

    i've watched this two times now, its just nicely explained and crafted . Thoroughly enjoyed it .

  5. SPACEUPHORIA September 10, 2019 at 10:45 pm - Reply

    tee. I don't know? lol It does what this glossary says it does. But, like you, I have no idea why they call it that.

    Return n independent iterators from a single iterable.

    The following Python code helps explain what tee does (although the actual implementation is more complex and uses only a single underlying FIFO queue).

    What's a FIFO queue?

  6. LemonadeFart September 10, 2019 at 10:45 pm - Reply

    ur mum gay

  7. Theja B.H September 10, 2019 at 10:45 pm - Reply

    SIR YOU ARE GOD GGGGOOOOOOOODDDDDDDDDDDD OF PYTHON YOU ARE THE BESSSSSSSSSSSSSSST YOU R THE ONE I WAS SEARCHING FOR. YOU MADE MY LIFE BRIGHT THANK YOU THANKYOU THANKYOU THANK YOU

  8. Mihai Dinca September 10, 2019 at 10:45 pm - Reply

    why it does not work simply daily_data = zip(counter, data) ?

  9. IntuitiveLeap September 10, 2019 at 10:45 pm - Reply

    I can see itertools.takewhile() and dropwhile() being very useful when dealing with iterators outputting dates (or part-numbers, or w/e) that you may or may not want to ignore, based on your custom logic.

    Rock on with your tuts. They are by far the best on youtube.

  10. Mano Meter September 10, 2019 at 10:45 pm - Reply

    Amazing teach video! Wow!

  11. haixu yan September 10, 2019 at 10:45 pm - Reply

    how does he make this in pycharm?  like [1,2, 3,4] =>[1 , 2 , 3 , 4]

  12. Meosh September 10, 2019 at 10:45 pm - Reply

    The tee command is named after the T-splitter in plumbing, which splits water into two directions and is shaped like an uppercase T.
    Suppose you wish to get the number of files in your directory and also store them in a new file.
    Intuitively, you may come up with something as this, ls -l > listOfFiles.txt | wc -l
    But there's a problem. All the output of ls -l is redirected to creating listOfFiles.txt file. So, the first part works really well. And there's for there's nothing 'left' to redirect to the 'wc -l' command, since, redirection to a file doesn't really return anything.

    To achieve what we wanted to achieve, we pass the output of ls -l to 'tee'. Tee does two things, writes to the file whose name is mentioned after the tee command and also sends it to command that comes after.

    ls -l | tee listOfFiles.txt | wc -l

    This would display the number of lines that ls – l outputs, while at the same time writing the list of files to the file 'listOfFiles.txt'.
    Once you get what it's trying to achieve it becomes easier to apply. Just remember when you send something to tee, it would split it two ways, 1) to the file, 2) to the next command in place.

  13. Peter Hornik September 10, 2019 at 10:45 pm - Reply

    your lt_2 function is kind of silly, you could just do def lt_2(n): return n<2… good stuff otherwise

  14. Shivam Singh September 10, 2019 at 10:45 pm - Reply

    Jane doe lives in kings landing, lucky woman😂😂

  15. Kev Obispo September 10, 2019 at 10:45 pm - Reply

    combinations_with_replacement vs product shows different results though… Just noticed..

  16. Lowzenza September 10, 2019 at 10:45 pm - Reply

    Is there a way to merge strings that overlap? This feature would be great since I work with DNA sequences and I have to manually fill in gaps from other files sometimes.

  17. Idan Refaeli September 10, 2019 at 10:45 pm - Reply

    Such a high standard video! Appreciated!

  18. Mr Cartoonster September 10, 2019 at 10:45 pm - Reply

    All… I… See… Is……………. Stars…. this video if full of stars…

  19. Josef Frühauf September 10, 2019 at 10:45 pm - Reply

    Hey, just want to say that I self-learned Python and your videos were a great help to me when I first started out. Now, I just remembered the channel and came over to see whether it still continues, and your teaching quality has somehow yet improved! Many thanks, and please keep doing this. I learned a lot in this video even though I already knew itertools. Thanks again, and cheers to many more lessons!

  20. Iqbal Syed September 10, 2019 at 10:45 pm - Reply

    Hi Corey, Just a quick query, something I couldn't figure out myself.
    Using an iterator (file reader), is it possible to search random lines (picked from another list which is not sorted)? If it is possible I will append those lines to a new list which will house all my randomly selected questions from a text file.

  21. Nur a Shams September 10, 2019 at 10:45 pm - Reply

    hello, brother Corey . i have some issue with groupby function. the issue is in the data (people) list if i mix the position among dictionaries , i means not maintaining the sequences of states name , the function doesn't group them properly rather generates individual iterable function objects for each dictionary , it should have group them together..
    in case of

    {
    'name': 'John Doe',
    'city': 'Gotham',
    'state': 'NY'
    },
    {
    'name': 'Corey Schafer',
    'city': 'Boulder',
    'state': 'CO'
    },
    {
    'name': 'Jane Doe',
    'city': 'Kings Landing',
    'state': 'NY'
    },
    the function doesn't group them..rather returns individual iterable objects

  22. Nur a Shams September 10, 2019 at 10:45 pm - Reply

    thank u bro.. could u pls make a vedio for socket module?

  23. Станислав Керимов September 10, 2019 at 10:45 pm - Reply

    Thank you very much! You are describing so clear and understandable!

  24. Minko Terziyski September 10, 2019 at 10:45 pm - Reply

    I don't know, why you sharing all this knowledge, but I am extremely, extremely thankful!

  25. Johnathan Ingle September 10, 2019 at 10:45 pm - Reply

    Thank you so much for your all your work. You are the best teacher ever 🙂

  26. Петя Табуреткин September 10, 2019 at 10:45 pm - Reply

    15:30 why can't you just write map(pow, *iterable)?

  27. Socio Logie September 10, 2019 at 10:45 pm - Reply

    good job Corey!

  28. Sonny September 10, 2019 at 10:45 pm - Reply

    Great video as always, buddy.

  29. dell latitude September 10, 2019 at 10:45 pm - Reply

    hi sir Corey 4 weeks no video ?
    please let me know if you are not uploading more videos so i can stop visiting your channel on daily basis. i am following you because i found your videos are best then others but if you will not upload regularly then it has no meaning to be best 🙁 Please update me in comment. when you will upload next video about python or on a python series or django ? because i feel sad when i didn't find new video by you

  30. rahul parmar September 10, 2019 at 10:45 pm - Reply

    videos on problem solving skills..plzz,, How to improve that high level problems

  31. General220 September 10, 2019 at 10:45 pm - Reply

    Hey, Corey, I'm a python beginner and I've started watching your python tutorials a few days ago. And I just wanted to tell you that you're a great teacher. I wish that I had found out about you 3 months ago when I started. I'm a Treehouse student, I like it there but they don't really explain as well as you do. So thank you, Corey, I appreciate your great work.

  32. Nawendu Singh September 10, 2019 at 10:45 pm - Reply

    200k congrats !!!!!

  33. Sandy September 10, 2019 at 10:45 pm - Reply

    if you are doing this full time then why are the videos published so intermittently ?

  34. marsras September 10, 2019 at 10:45 pm - Reply

    Hi Corey,
    I find your videos very interesting, do you have any videos about “argparse” in python?

  35. Ra-ki Papa September 10, 2019 at 10:45 pm - Reply

    hands down, these are the most in-depth python tutorial materials I've ever gone through. Thanks, buddy.
    subscribe++
    like++
    recommend_to_friends = "ABSOLUTELY YES"

  36. Aditya Rawat September 10, 2019 at 10:45 pm - Reply

    please do a video on Collections module

  37. PuppySacks September 10, 2019 at 10:45 pm - Reply

    Hi, I’m new to your channel and I’d rlly like to say thank you for making these videos! It has rlly given me the encouragement I needed to continue to learn. The way you walk us through the process, to where it’s easy enough for a beginner like me, I’m grateful.

  38. Nex September 10, 2019 at 10:45 pm - Reply

    Hi Corey, I love the videos but could you please to a tutorial on the socket module?

  39. py lvr September 10, 2019 at 10:45 pm - Reply

    thanks corey , can t wait your next video

Leave A Comment

*