#309: Repeating and Combining Lists With itertools
One of the things I like the most of Advent of Code is that it forces us to learn more about the language we use. This year I had to do a lot of combinations of elements in a list and for that we can either write the boring code on our own or use the itertools module that ships with Python. In this post we explore the most useful features of this hidden gem.
No installation needed
The itertools module is part of Python. Therefore, we can skip the installation and directly import it in our scripts:
Cycle through a list forever
If we need to go through our list and start over when we reach the end, we can use the cycle() function to create an endless iterator:
To stop the iterator, we need to use CTRL-Z.
When we have a string, we can directly use it with the itertools and do not need to convert it into a list:
Cycle through a list to get X values
If we know how many values we need, we can use the generated iterator of the cycle() function like this:
That way we get a lot of flexibility while keeping our code short.
Combine all elements of a single list
If we need to combine every element in a list with each other and the order does not matter, we can use the combinations() function to get the desired output:
If we need larger groups, we can increase the length parameter:
Combine all elements of a list where order matters
When we need a combination of all elements in our list but (1,2) and (2,1) are not the same, we can use the permutations() function:
Create the product of two lists
If we have two lists and want to create the Cartesian product (all tuples combining one element from each iterable), we can use the product() function:
Conclusion
Combining values of lists or creating iterators that repeat the values for as long as we need them may not be things we need every day. But when we have a problem that requires such a behaviour, it is good to know that Python has functions for exactly that. The functions covered by this blog post are just the most useful ones from the itertools module. I strongly recommend you check for yourself what other helpful functions this module offers - it may save you a lot of time.