0

In Python is there a way to get all 3 pairs or n pairs of a list from a list?

For example list = [1,2,3,4]
result : [[1,2,3],[2,3,4],[1,2,4]]

I want to find all possible n pairs of lists from a Python list. I do not want to import any other functions like itertools.

karel
  • 5,489
  • 46
  • 45
  • 50
  • I don't understand what do you mean. Could you explain yourself better? – Ender Look Mar 22 '19 at 03:20
  • Welcome to Stack Overflow! Please take our [tour], it only takes a minute, I promise. Then, if you have some time check our [help] for a better understanding of the site itself. – Ender Look Mar 22 '19 at 03:23
  • I want to find all possible n pair of lists from a list. from my example I want to find all possible 3 pair of lists from list [1,2,3,4] then result will be [[1,2,3],[2,3,4],[1,2,4]] –  Mar 22 '19 at 03:26
  • 1
    Possible duplicate of [How to generate all permutations of a list in Python](https://stackoverflow.com/questions/104420/how-to-generate-all-permutations-of-a-list-in-python) – Ender Look Mar 22 '19 at 03:40

1 Answers1

0

You can use the module itertools. It comes inside Python by default (you don't need to install it throught third party modules):

>>> import itertools
>>> print(itertools.permutations([1,2,3,4], 3))
[(1, 2, 3), (1, 2, 4), (1, 3, 2), (1, 3, 4), (1, 4, 2), (1, 4, 3), (2, 1, 3), (2, 1, 4), (2, 3, 1), (2, 3, 4), (2, 4, 1), (2, 4, 3), (3, 1, 2), (3, 1, 4), (3, 2, 1), (3, 2, 4), (3, 4, 1), (3, 4, 2), (4, 1, 2), (4, 1, 3), (4, 2, 1), (4, 2, 3), (4, 3, 1), (4, 3, 2)]

This itertools.permutations(iterable, r=None) produces all the possible permutations of a given iterable element (like a list). If r is not specified or is None, then r defaults to the length of the iterable and all possible full-length permutations are generated.

If you are looking only for n pair of permutations instead of all the possible permutations just delete the rest of them:

>>> print(list(itertools.permutations([1,2,3,4], 3))[:3])
[(1, 2, 3), (1, 2, 4), (1, 3, 2)]

As you asked in comments you can do that without importing any module. itertools.permutations is just a function, which you can make by yourself:

def permutations(iterable, r=None):
    pool = tuple(iterable)
    n = len(pool)
    r = n if r is None else r
    if r > n:
        return
    indices = list(range(n))
    cycles = list(range(n, n-r, -1))
    yield tuple(pool[i] for i in indices[:r])
    while n:
        for i in reversed(range(r)):
            cycles[i] -= 1
            if cycles[i] == 0:
                indices[i:] = indices[i+1:] + indices[i:i+1]
                cycles[i] = n - i
            else:
                j = cycles[i]
                indices[i], indices[-j] = indices[-j], indices[i]
                yield tuple(pool[i] for i in indices[:r])
                break
        else:
            return

But I strongly advise your importing it. If you don't want to import the whole module, just this function simply do from itertools import permutations.

Ender Look
  • 2,303
  • 2
  • 17
  • 41
  • is there other way to do it without import some function? –  Mar 22 '19 at 03:35
  • @eumsangwon of course, just replicate the code of that function by your own. I have posted its code in my post, but I strongly advise you importing this module. If you don't like to import the whole library just import this single function doing `from itertools import permutations`. Then you could use it as `permutations(myList)` – Ender Look Mar 22 '19 at 03:38
  • @eumsangwon Can I ask why you don't want to import it? – Ender Look Mar 22 '19 at 03:39