Python Itertools For Advanced Iteration

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] what is going on guys welcome back this video is more targeted to beginners because today i want to show you a core python module called ethertools that is going to make a lot of things that are related to lists and counting and combinatorics uh easier for you you don't have to do them from scratch you can just use this core python module called editor tools to make things easier and simpler to use so for those of you who are new to python or just starting to get more intermediate this might be quite interesting so let us get right into it all right so let's get started by importing itter tools as i already mentioned editor tools is part of the core python stack so we don't need to install anything all we need to do is we need to type import editor tools in this module this library provides us with a lot of useful functionality when it comes to collections when it comes to iterating over collections as the name already says editor tools when it comes to combinatorics and so on and we're going to start by looking at a quite simple function called the count function which is similar to a function you might know called range but not quite the same and the idea is that sometimes in python we want to start with i being equal to 0 for example while true so basically an endless loop we want to print i and we want to increase i by 5 for example which is the step size and of course if we run this this is going to go on forever so we have to terminate it here by force of course we can also provide a limit we can say okay if i is equal to 5000 then break so this works but then of course if we do it like that if we don't want to have an endless loop we can also just use the range function we can just say 4i in range starting at 0 going up until 5000 with a step size of five print i so this is not either tools this is just basic python you probably know this already um but the idea is quite similar because range actually produces a generator where we can get the next element and by iterating over it we get the next element all the time until there is no element anymore and with editor tools we can do a similar thing using the count function so we can say 4i in itter tools.count starting at 0 with a step size of 5 you can see the maximum is missing because this is a generator a real generator generating more and more values without a limit i have a video on generators if you want to take a look at that it's part of my advanced python tutorial series i think or of my python tips and tricks but i think it's the advanced python tutorial series and essentially i can print your eye now and this is going to go on forever because the generator constantly produces new elements and we can see that this works like a generator by um going ahead and initializing it let's say counter equals itertools count zero five and now if i go ahead and i print next which is a typical function that you use on generators uh we're gonna get the next element all the time so 0 5 10 15 20 and you will not be able to turn this into a list of course which is possible with a range because if i have print list range 0 5005 this is just going to turn this into a list with all the elements if i try to call list on the counter for example this is going to be an endless loop because this list does not have as you can see this is still running here this list does not have boundaries it goes on up until infinity so this can be quite useful if you want to have an infinite counter where you can always get a next element you don't want to specify a limit um so you want to have a start point and a step size so 0 5 10 15 and so on until you don't don't need it anymore it's never going to be empty you can use it as long as you want so this is one thing that editor tools offers another one is the cycle function the cycle function is quite interesting let's say we have a set of elements and these elements are for example a and then we have 20 and then we have 4.5 and then we have true maybe just a bunch of values here and now i want to have a cycle so i want to use these four elements in that order all the time i want to iterate but not only once because of course i can go ahead and say 4i in elements and then i can print i um but this essentially doesn't happen forever i cannot do this over and over again of course i can do something messy i think like doing a while true which is very a very bad solution but i can do that if i want to a more elegant solution would be to do something like 4i in range of length of elements and then i can just go i modulo 3 or in this case 4 which is the length of elements and then i can just increase um actually this then would have to be actually let's do wow true they go i plus equals one and i starts as zero so this would work for example um and of course we don't want to print i we want to print elements whatever we get here there you go so this would also work but this is not an elegant solution and of course it only works in a while true it doesn't work by just getting the next element until we don't need one anymore so if you want to do that professionally with editor tools you can use a cycle function the basic idea is to say 4i in itter tools dot cycle and we pass the elements and then we print i like that so this works like that and we can also do something like my cycle it's gonna be itertools.cycle elements and then i can just print next of my cycle and then we get the next element all the time so we basically go a 20 4.5 through a 20 4.5 true and so on and so forth all the time in a cycle which can be useful sometimes sometimes we have elements we want to reiterate we want to go in circles all the time um and we we don't want to have to specify how many times we do that we also don't want to go in endless loop we want to use this next to always have uh the next element so this allows us to iterate over a collection in a cycle way so to say so another interesting function is the cartesian i i hope it's pronounced cartesian cartesian product essentially you have two collections so let's say we have numbers one and we have the values one two three and let's keep the pep8 conventions here and then you have four five six and what you wanna have now is the product where the the cartesian product where essentially you have prod equals and you want to have in this case like one and four you want to have two and four you want a three and four and one and five and so on and so forth and you can do that with two loops so you can just go ahead and say four number or actually four i in numbers so one four j in numbers two then you can just print here or not print actually we need to create an empty list prod equals empty list and then we're going to say prod append a tuple of ing and then we can print that so we can do it like that but we can also just use editor tools to make things simpler for us we can just print editor tools dot okay now i triggered some snippet itertools dot product and then we can pass numbers one numbers two and of course in this case we get this generator here so we need to turn it into a list to display it but there you go this is the exact same result here you just have to type cast to a list you have to to trigger the execution because you have this lazy execution this lazy evaluation where you have the information here but in order to actually trigger it to make it an actual list you need to call the list typecasting um you need to typecast to a list here but this is also a helpful thing that you can do now i want to show you two more things the rest i think this whole module is so interesting you can you can just go to documentation and try all the minor functions um i think i'm gonna show you two more because they're going to be quite interesting for combinatorics oftentimes we have elements and we want to know okay what kind of uh permutations we have combinations we have and so on and for that we have the actual uh permutation and combination function in itter tools which is quite useful so let's say we have elements and now we're going to call them just um a b c [Music] d and e and now i want to know k how can i arrange these elements and of course we can do that in our brain if we have like uh five elements here but oftentimes we have huge collections and we still wanna uh have the permutations so remember we have when it comes to permutations we have the length in this case five which is n uh n factorial permutations and what we can do to calculate those is we can just say print itertools.permutations of elements and what this does is again we need to type cast into a list to see the result but what this does is it gives us all the permutations here so basically i can have a b c to e i can have a b c e d i can have a b d c e and so on all the possible orders of these elements are given by the permutations and if we want to do some calculations on this set here on on all these permutations or we want to filter out some of those we don't have to to write the algorithm for that manually we can just use the permutations function of error tools and then we get such a collection same goes with the combinations the combination idea is the following i have a collection of abcde and let's say i pick three elements i i make three picks which three pick combinations can i make so for example let's say i pick a then a is out of the collection so i picked a now i can also pick d and d is out of the collection as well and now i pick c so this would be one combination a d c i picked first a then d then c now another one could be i pick e i pick b i pick c in order to get all these combinations i can just go ahead and say elements sorry actually print iter tools dot combinations of elements and i need to pass a second parameter here indicating how many i want to choose so in this case we talked about three and i think i'm gonna have to type cast this again into a list there you go and here now in the second row you can see all the possible scenarios i can go a b c a b d a b e a c d and so on and so forth but those are the combinations uh where the order doesn't matter so you can see here that those are all the combinations but we don't have anything starting with d for example because the order is not important in this case we just say that abc is the same as cba for example it just matters what kind of choices i made now let's say i want to model a scenario where i have the same the same thing so i have the same elements and i again want to pick one after the other i want to take three elements but let's say i have the replacement so when i pick an a it doesn't it doesn't vanish from the list so i basically i take an a out but there's also an a back in there so i can pick an a again uh if i want to mod that i can use the combinations with replacement function and then we get more stuff like aaa aab aac and so on so this is quite interesting again i don't want to go here through all the functions because there's no value in a video where i basically show you the whole module first of all you can just use auto completion to trigger them you can see here we have chain accumulate repeat and so on repeat is quite simple essentially you have a value and you repeat it for example 50 times and this creates a generator that repeats a value 50 times um but it is a quite interesting module that you can use it's a very basic one there's no reason to not use it because it's part of core python you don't even have to rely on external libraries so whenever you do something where it where where you have to iterate over something in a more advanced way where you want to do something with combinatorics either tools is a very nice library to use so that's it for today's video hope you enjoyed i hope you learned something if so let me know by hitting the like button leaving a comment in the comment section down below and of course don't forget to subscribe to this channel and hit the notification bell to not miss a single future video for free other than that thank you much for watching see you next video and bye [Music] you
Info
Channel: NeuralNine
Views: 9,830
Rating: undefined out of 5
Keywords: python, itertools, iteration, tutorial, python itertools, python itertools tutorial, collections
Id: m4vnmv_S6ug
Channel Id: undefined
Length: 13min 59sec (839 seconds)
Published: Sat Mar 26 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.