# itertools Module

In this lesson, you’ll learn about the `itertools`

module. This module helps you create iterators for efficient looping:

```
>>> import itertools as it
>>> rep = it.repeat(1)
>>> next(rep)
1
>>> next(rep)
1
```

You’ll also learn about:

, which infinitely cycles through iterables`itertools.cycle`

, which finds all the permutations (order matters)`itertools.permutations`

, which finds all the combinations (order does not matter)`itertools.combinations`

You can check out the Python documentation on the `itertools`

module.

**00:00**
In this video, you’ll learn about the `itertools`

module, which contains a lot of useful functions that return iterators that help us loop through sequences efficiently.

**00:09**
Let’s start by importing the `itertools`

module. I’m going to import `itertools`

like this, and alias it as `it`

just so I don’t have to type `itertools`

over and over again.

**00:19**
Let’s start with `itertools.repeat()`

. This is a useful function that takes in a number and returns an iterator that—when you call `next()`

on it—will return this number. Let’s call this `all_ones`

.

**00:32**
When you call `next()`

on `all_ones`

, it will print out `1`

over and over and over again. If you were to call `list()`

on `all_ones`

, this will run forever because `all_ones`

goes on infinitely.

**00:44**
Let’s see how you might use this. Let’s say we wanted to create a list of 0 through 10 and square each number. Instead of doing a list comprehension, you could use the built-in `map()`

and then the built-in `pow()`

function, pass in `range(10)`

, and then pass in our `itertools.repeat(2)`

This will create a `map`

object, and then `list()`

under the hood will call the `next()`

method on our `map`

object, and then return a list of 0 through 10—not including 10—squared.

**01:13**
You could change this to `3`

, or `4`

, or something like this. It’s just a easy way to have a constant stream of numbers that you need to keep accessing over and over in some sort of `list()`

or `map()`

.

**01:25**
You can also pass in a optional `times`

argument, and that way, when you cast it to a list, it will actually stop because there is a finite number of `1`

’s.

**01:34**
There’s also another useful method, `itertools.cycle()`

. It’ll take an iterable and return an iterator that will loop through the iterable infinite number of times. It does not take in an optional argument, so this will actually go forever. So, let’s do `alternating_ones`

, which is the cycle of `1`

, `-1`

. Calling `next()`

on `alternating_ones`

,

**01:56**
`1`

, `-1`

, `1`

, `-1`

, `1`

, `-1`

. So, sometimes in an interview, you need some sort of infinite sequence—either of a constant number, or have maybe a sequence of numbers that you need to keep repeating over and over until some condition happens.

**02:09**
This is a useful way to do it in one line. Let’s look at another function that is probably more useful in interviews: `it.permutations()`

. So, this does exactly what you think it does.

**02:20**
It finds all the permutations of a specific length. I’m going to clear the output, just so it’s a little bit cleaner. Let’s say we have `friends = ["Jack", "Jill", "Joe"]`

.

**02:32**
You can do `it.permutations()`

of `friends`

**02:37**
and then you pass in the length, `2`

—so this will find all the permutations of length `2`

. `permutations()`

does care about order, so `('Jack', 'Jill')`

and `('Jill', 'Jack')`

will appear twice.

**02:47**
This will return an iterator and then cast it to a list, and you get all the permutations. Length `3`

. Length `4`

is nothing because there’s no permutation of length `4`

. You might

**03:00**
be thinking, “Okay, I don’t care about order, and I want `('Jack', 'Jill')`

and `('Jill', 'Jack')`

only to appear once.” `it.combinations()`

, `friends`

, grouping of `2`

. And grouping of `3`

.

**03:11**
You might want to ask your interviewer if it’s okay to use these built-in functions because sometimes they want you to implement it from scratch. There are many examples online how to do that, but sometimes it’s really useful to do this maybe in a coding challenge, or if your interviewer just wants you to get the permutations and wants you to do some more complex logic on them.

**03:31**
There are a ton more functions in the `itertools`

module and I’ll link them all below. In the next video, you’ll learn about the `functools`

module, which includes a lot of useful higher order functions and decorators such as the higher order function `reduce()`

and the decorators `lru_cache`

and `cached_property`

.

**Pygator** on April 30, 2020

Lot’s of helpful hints/tips, but talking so fast does not help me learn it faster.

**drawdoowmij** on May 4, 2020

Hi James – Great video, at 1:10 you say “list calls the next method on a map object” can you let me know where in the documentation you found this. Thanks!

**James Uejio** RP Team on May 8, 2020

Hi @Pygator I will keep my speed in mind in the future, but for the current videos you can go to the gear icon and select 0.75x speed if it is too fast for you.

@drawdoowmij I can’t really find Python documentation on where this happens but I learned it in school here: www.youtube.com/watch?v=On-kFyFp8HY&list=PL6BsET-8jgYWPYsrAfTAN8rvBzk5cYfUZ&index=2. You can also see it answered here stackoverflow.com/questions/34732641/calling-list-empties-my-iterable-object. Plus you can see in the following example that list has to call next because it empties the iterator

```
>>> iterator = iter([1,2,3])
>>> next(iterator)
1
>>> list(iterator)
[2, 3]
>>> next(iterator)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
```

**drawdoowmij** on May 8, 2020

Thanks for this information James!

Become a Member to join the conversation.

James UejioRP Team on April 27, 2020Here is the Python documentation on the

`itertools`

module: Python itertools module