How to write a recursive function that takes a list and return the same list without vowels?
<p>I am supposed to write a recursive function that takes a list of strings or a list of lists of strings and return the list without vowels, if found. Here is my attempt to solve it:</p>

<pre><code>def noVow(seq):
    keys = ['a','i','e','o','u','u']
    if not seq or not isinstance(seq, list) :
        return 
    else:
        if seq[0]  in keys:
            del seq[0]
            return (noVow(seq[0:]))
        else:
            return (noVow(seq[1:]))


li = ["b", "c", "d","a"]
print (noVow(li))
</code></pre>

<p>I am aware that the bug lies in my base case however I can't come up with the right base case. </p>

<p>Note that the recursive function has to be written in pure functional programming i.e. side effects are not allowed.</p>

<blockquote>
  <p>return the <strong>same</strong> list without vowels</p>
</blockquote>

<p>Eh, you're <em>slicing</em> the original list in the recursive calls, so you have a <strong>copy</strong> not the same list. </p>

<p>More so, your code actually works, but since you're passing a <em>slice</em> of the list, the vowel items in the slice (not the original list) are deleted and the original remains unchanged. </p>

<p>You can instead use a <em>non-slicing</em> variant that moves from <em>start</em> to <em>end</em> indices of the original list:</p>

<pre><code>def no_vow(seq, index=0):
    keys = ['a','i','e','o','u']
    if not seq or not isinstance(seq, list) or index &gt;= len(seq):
        return 
    else:
        if seq[index] in keys:
            del seq[index]
            return no_vow(seq, index)
        else:
            return no_vow(seq, index+1)
</code></pre>

<p>Finally, if you're going to print your result, you shouldn't print the output of the function call (which will be <code>None</code>) but the list.</p>

<hr>

<p><strong>Trial</strong>:</p>

<pre><code>li = ["b", "c", "e", "d", "a"]

no_vow(li) # list is modified in-place
print(li)
# ["b", "c", "d"]
</code></pre>

python recursion functional-programming