0

I'm trying to retrieve every combination of a list's elements in a variable but one at a time, I used this code that I modified to fit my program :

import itertools
stuff = [1, 2, 3]
for L in range(0, len(stuff)+1):
    for subset in itertools.combinations(stuff, L):
        print(subset)

in my program, the list "stuff" contains the alphabet and numbers, but the problem is that I retrieve the combinations like "a", "b",..., "y", "z" and then i would like to get "aa", "ab", "ac",... but it doesn't return "aa", as if it skipped it, it comes to "ab" directly.

I'm new to this forum, it's my first question so it might not be perfect
Thanks for any help :)

Kalusd
  • 3
  • 1
  • 1
    Does this answer your question? [Generating permutations with repetitions](https://stackoverflow.com/questions/3099987/generating-permutations-with-repetitions) – Jeppe Mar 01 '22 at 22:24
  • 1
    Does this answer your question? [Get all possible combination of values in a list -- Python](https://stackoverflow.com/questions/62535439/get-all-possible-combination-of-values-in-a-list-python) – ksohan Mar 01 '22 at 22:24
  • 1
    You want [`combinations_with_replacement`](https://docs.python.org/3/library/itertools.html#itertools.combinations_with_replacement) (or [`product`](https://docs.python.org/3/library/itertools.html#itertools.product) if both "ab" and "ba") – mozway Mar 01 '22 at 22:25
  • Can you give your expected output for `[1,2,3]`? – Brendan Abel Mar 01 '22 at 22:32

1 Answers1

1

You're not looking for combinations of elements from an existing collection, but for all possible products of the elements of that collection:

import itertools

stuff = ['a', 'b', 'c']
for n in range(0, len(stuff) + 1):
    for result in itertools.product(stuff, repeat=n):
        print(result)

Output:

()
('a',)
('b',)
('c',)
('a', 'a')
('a', 'b')
('a', 'c')
('b', 'a')
('b', 'b')
('b', 'c')
('c', 'a')
('c', 'b')
('c', 'c')
('a', 'a', 'a')
('a', 'a', 'b')
('a', 'a', 'c')
('a', 'b', 'a')
('a', 'b', 'b')
('a', 'b', 'c')
('a', 'c', 'a')
('a', 'c', 'b')
('a', 'c', 'c')
('b', 'a', 'a')
('b', 'a', 'b')
('b', 'a', 'c')
('b', 'b', 'a')
('b', 'b', 'b')
('b', 'b', 'c')
('b', 'c', 'a')
('b', 'c', 'b')
('b', 'c', 'c')
('c', 'a', 'a')
('c', 'a', 'b')
('c', 'a', 'c')
('c', 'b', 'a')
('c', 'b', 'b')
('c', 'b', 'c')
('c', 'c', 'a')
('c', 'c', 'b')
('c', 'c', 'c')

Note: I renamed L to n, because it's generally not a good idea to name variables and functions in Python with capitals and I renamed subset to result because what both combinations and product produce are not sets but tuples.

As @mozway correctly points out, you may consider an example like ('a', 'b') and ('b', 'a') duplicates, in which case you want combinations_with_replacement:

import itertools
stuff = ['a', 'b', 'c']
for n in range(0, len(stuff) + 1):
    for result in itertools.combinations_with_replacement(stuff, r=n):
        print(result)

Output:

()
('a',)
('b',)
('c',)
('a', 'a')
('a', 'b')
('a', 'c')
('b', 'b')
('b', 'c')
('c', 'c')
('a', 'a', 'a')
('a', 'a', 'b')
('a', 'a', 'c')
('a', 'b', 'b')
('a', 'b', 'c')
('a', 'c', 'c')
('b', 'b', 'b')
('b', 'b', 'c')
('b', 'c', 'c')
('c', 'c', 'c')
Grismar
  • 27,561
  • 4
  • 31
  • 54
  • 2
    Or [`combinations_with_replacement`](https://docs.python.org/3/library/itertools.html#itertools.combinations_with_replacement), the question is unclear. – mozway Mar 01 '22 at 22:30