title: Recursive function that returns combinations of size n chosen from list
body: <p>I am trying to write a recursive function which takes as its inputs an integer <code>n</code>, and a list <code>l</code>, and returns a list of all the combinations of size n that can be chosen from the elements in <code>l</code>. I'm aware that I can just use itertools, but I want to become better at writing recursive functions, and I believe writing my own function will help me.</p>
  <p>So for example, if you input:</p>
  <blockquote>
  <p><code>n = 3</code></p>
  <p><code>l = [1, 2, 3, 4]</code></p>
  </blockquote>
  <p>I want the output to be:</p>
  <blockquote>
  <p>`[ [1, 2, 3],  [1, 3, 4],  [2, 3, 4], [1, 2, 4] ]</p>
  </blockquote>
  <p>So far I've written this code:</p>
  <pre><code>def get_combinations(l, n): # returns the possible combinations of size n from a list of chars
  
      if len(l) == 0:
          return []
  
      elif n == 1:
          return [l[0]]
  
      newList = list()
  
      for i in range(len(l)):
      
          sliced_list = l[i:]
          m = n - 1
  
          #lost here, believe I need to make a recursive call somewhere with get_combinations(sliced_list, m)
  
      return newList
  </code></pre>
  <p>I found <a href="https://www.geeksforgeeks.org/generate-all-the-permutation-of-a-list-in-python/" rel="nofollow noreferrer">this example of such a function for permutations</a> helpful, but I'm struggling to implement something similar.</p>
  <p>To clarify, I set up my base cases in the way I did because I'm expecting to pass <code>sliced_list</code> and <code>m</code> in my recursive call, and if you imagine the situation when <code>i = 3</code>, you'll have an empty list for <code>sliced_list</code>, and <code>m</code> will be 1 when you've gone deep enough to build up a combination. But I'm not married to these base cases.</p>
  <p>Let me try to summarize the questions I have:</p>
  <ol>
  <li><p>How do I produce a final result which is exactly a list of lists, instead of a list of lists of lists of lists ... (depth = n)?</p>
  </li>
  <li><p>What should my recursive call look like?</p>
  </li>
  <li><p>Am I going about this problem in exactly the wrong way?</p>
  </li>
  </ol>