0

I need to make all possible combinations of any length of list items without using itertools.

  • Example: list = [1,2,3]

  • Output: [[1],[2],[3],[1,2],[1,3],[2,3],[1,2,3]] ( not necessarily in lexicographical order)

I have this code:

def get_combination(list_of_ palindrome):

res = [list_of_palindrome[i: j] for i in the range (len(list_of_palindrome)) for j in the range (i + 1, len(list_of_palindrome) + 1)] 

return res

Output: [[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]

azro
  • 47,041
  • 7
  • 30
  • 65
Tamada
  • 33
  • 6
  • To the person who closed, OP did specify **without using `itertools`** and the top few (all?) answers of linked duplicate are *all* using `itertools`... – Joshua Voskamp Nov 11 '21 at 18:45
  • 1
    have a look at https://stackoverflow.com/a/54480126/4871001 -- that's a sol'n without using itertools – Joshua Voskamp Nov 11 '21 at 18:46
  • If input is not unique then output may not be unique either, of course this may or may not be a problem but just in case it matter it is worth specifying. – DaveQ Nov 11 '21 at 19:13
  • `def allComb(A): return [A[:1],*(A[:1]+c for c in allComb(A[1:])),*allComb(A[1:])] if A else []` – Alain T. Nov 11 '21 at 20:59

0 Answers0