# Itertools

{% embed url="<https://www.hackerrank.com/domains/python?badge_type=python&filters%5Bsubdomains%5D%5B%5D=py-itertools>" %}

## itertools.product()

<figure><img src="https://3452970062-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhMewGT4PbQ5e1zQPC4Ko%2Fuploads%2FORGHY52dDcqG6zl9QdOk%2Fimage.png?alt=media&#x26;token=57be65e2-e2dd-49b8-b329-208b2c014e5f" alt=""><figcaption></figcaption></figure>

{% code lineNumbers="true" %}

```python
from itertools import product

A = map(int, input().split())
B = map(int, input().split())

print(" ".join(map(str, product(A, B))))
```

{% endcode %}

***

## itertools.permutations()

<figure><img src="https://3452970062-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhMewGT4PbQ5e1zQPC4Ko%2Fuploads%2FFekgggd2Q20HT4UJtDj7%2Fimage.png?alt=media&#x26;token=3a89adac-e264-4809-9270-c428ad3bfabf" alt=""><figcaption></figcaption></figure>

```python
from itertools import permutations

s, k = input().split()

print("\n".join(sorted(map("".join, permutations(s, int(k))))))
```

***

## itertools.combinations()

<figure><img src="https://3452970062-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhMewGT4PbQ5e1zQPC4Ko%2Fuploads%2FKIs3s9uTAgIakAMKHiDp%2Fimage.png?alt=media&#x26;token=b42a72f5-05a3-4115-81b5-9adf5d06adf2" alt=""><figcaption></figcaption></figure>

```python
from itertools import combinations

s, k = input().split()

for i in range(int(k)):
    print("\n".join(sorted("".join(sorted(each)) for each in combinations(s, i + 1))))
```

***

## itertools.combinations\_with\_replacement()

<figure><img src="https://3452970062-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhMewGT4PbQ5e1zQPC4Ko%2Fuploads%2FZ7llA2zeZcAVdALZrqmB%2Fimage.png?alt=media&#x26;token=470bd8b8-63f1-4858-a26b-9e14f9c59e73" alt=""><figcaption></figcaption></figure>

```python
from itertools import combinations_with_replacement

s, k = input().split()

print("\n".join(sorted("".join(sorted(each)) for each in combinations_with_replacement(s, int(k)))))
```

***

## Compress The String!

<figure><img src="https://3452970062-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhMewGT4PbQ5e1zQPC4Ko%2Fuploads%2Fc6OuUkzYEU77KoeHY89C%2Fimage.png?alt=media&#x26;token=1c3e53a6-b6fe-4d8a-a5f4-f6e631e6427d" alt=""><figcaption></figcaption></figure>

```python
from itertools import groupby

s = input()

print(" ".join(str((len(list(group)), int(value))) for value, group in groupby(s)))
```

***

## Iterables and Iterators

<figure><img src="https://3452970062-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhMewGT4PbQ5e1zQPC4Ko%2Fuploads%2FsHhLr8ayUcosw9t4JdXB%2Fimage.png?alt=media&#x26;token=b36e9f22-110c-4d07-bafc-dd70dd059f8e" alt=""><figcaption></figcaption></figure>

```python
from itertools import combinations

n = int(input())
letters = input().split()
k = int(input())

combos = list(combinations(letters, k))
print(len(list(each for each in combos if "a" in each)) / len(combos))
```

***

## Maximize It!

<figure><img src="https://3452970062-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhMewGT4PbQ5e1zQPC4Ko%2Fuploads%2FaWJJCzZFZcVZxtLTrp9I%2Fimage.png?alt=media&#x26;token=157c8473-4f40-4d9f-a73c-064a0ff0307b" alt=""><figcaption></figcaption></figure>

```python
from itertools import product

k, m = map(int, input().split())

arr = [list(map(lambda x: pow(int(x), 2), input().split()[1:])) for _ in range(k)]

print(max(map(lambda x: sum(x) % m, product(*arr))))
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://thamizhiniyancs.gitbook.io/writeups/hackerrank/python/itertools.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
