1

Im trying to iterate through all possibilities of a list, where the size of the resulting lists can be greater than the size of list of variables.

For example, if given ([1,-1], 3) the result would be:

[1,1,1], [1,1,-1], [1,-1,1], [1,-1,-1], [-1,1,1], [-1,1,-1], [-1,-1,1], [-1,-1,-1]

My code for this was using itertools but the function only outputted a fraction of what was actually supposed to be outputted. My code:

def perms(list1, size):
    for permutation in itertools.combinations_with_replacement(list1, size):
        print(permutation)
QWERGHN
  • 11
  • 1
  • try here: https://stackoverflow.com/questions/104420/how-to-generate-all-permutations-of-a-list – Kyle Hurst Feb 23 '21 at 17:36
  • Could you give us more example inputs and outputs? It is not clear to me what your code should output for e.g. `([1,2,3], 5)`. – apilat Feb 23 '21 at 17:42
  • You're looking for [`product`](https://docs.python.org/3.7/library/itertools.html#itertools.product): `itertools.product([1, -1], repeat=3)` – Tomerikoo Feb 23 '21 at 17:47
  • Does this answer your question? [How to generate all permutations of a list?](https://stackoverflow.com/questions/104420/how-to-generate-all-permutations-of-a-list) – Tomerikoo Feb 23 '21 at 17:52
  • 1
    @KyleHurst If you think this question has an answer somewhere else in this site - [flag it as duplicate](https://stackoverflow.com/help/privileges/flag-posts) instead of posting a link as a comment... – Tomerikoo Feb 23 '21 at 17:54

0 Answers0