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>

<p>To work for a list of lists containing string and a flat list of strings, you need to iterate over the sequence and then check the type:</p>

<pre><code>def noVow(seq):
    vowels = {'a', 'i', 'e', 'o', 'u', 'u'}
    for ele in seq:
        if isinstance(ele, list):
            # a list so recursively process
            yield [s for s in noVow(ele)]
        # else it has to be a string so just see if it is not a vowel
        elif ele not in vowels:
            yield ele
</code></pre>

<p>You use it like:</p>

<pre><code>In [39]: li
Out[39]: [['b', 'c'], ['d', 'a']]

In [40]: li[:] = noVow(li)

In [41]: print(li)
[['b', 'c'], ['d']]

In [42]: li = ["a","b","c","e"]

In [43]: li[:] = noVow(li)

In [44]: print(li)
['b', 'c']
In [10]: li = [["b", "c"], ["d", ["a"]]]
In [11]: li[:] = noVow(li)   
In [12]: li
Out[12]: [['b', 'c'], ['d', []]] # works for nested lists
</code></pre>

<p>If you wanted a flat list of all non-vowels and you can use python3, you can use <em>yield from</em>:</p>

<pre><code>def noVow(seq):
    vowels = {'a', 'i', 'e', 'o', 'u', 'u'}
    for ele in seq:
        if isinstance(seq, list):
            yield from noVow(ele)
        elif ele not in vowels:
            yield ele
</code></pre>

<p>You use it the same way:</p>

<pre><code>In [2]: li = [["b", "c"], ["d", "a"]]

In [3]: li[:] = noVow(li)

In [4]: li
Out[4]: ['b', 'c', 'd']

In [5]: li = ["a","b","c","e"]

In [6]: li[:] = noVow(li)

In [7]: li
Out[7]: ['b', 'c']
</code></pre>

<p>You can do the same with python2, you just need another loop</p>

python recursion functional-programming