How can I get this Python function to return a string instead of a list containing one string?
<p>How can I get this Python function to return a string instead of a list containing one string?</p>

<pre><code>def num_vowels(s):
    """ returns the number of vowels in the string s
        input: s is a string of 0 or more lowercase letters
    """
    if s == '':
        return 0
    else:
        num_in_rest = num_vowels(s[1:])
        if s[0] in 'aeiou':
            return 1 + num_in_rest
        else:
            return 0 + num_in_rest

#most vowels returns a list not a string
def most_vowels(wordlist):
    '''Takes a list of strings called wordlist and returns the string in
       the list with the most vowels.'''
    list_of_num_vowels = [num_vowels(x) for x in wordlist]
    max_val =  max(list_of_num_vowels)
    return [x for x in wordlist if num_vowels(x) == max_val] 
</code></pre>

<p>Some test cases:</p>

<pre><code>most_vowels(['vowels', 'are', 'amazing', 'things'])
</code></pre>

<p>output: <code>'amazing'</code></p>

<pre><code>most_vowels(['obama', 'bush', 'clinton'])
</code></pre>

<p>output: <code>'obama'</code></p>

<p>Thanks!</p>

<p>your <code>most_vowels()</code> function is using a <a href="http://www.secnetix.de/olli/Python/list_comprehensions.hawk" rel="nofollow">list comprehension</a> to filter out matching values and return a new list. You could output the string directly in two ways.</p>

<p>The first way is to index the first result from the result of the comprehension.</p>

<pre><code>return [x for x in wordlist if num_vowels(x) == max_val][0]
</code></pre>

<p>The other option, if you think you may get multiple results but still want a string, is to join any results with a comma (or other separator) into a string.</p>

<pre><code>return ','.join([x for x in wordlist if num_vowels(x) == max_val])
</code></pre>

<p>This will convert <code>['first', 'second']</code> to <code>'first,second'</code>.</p>

python string