My if statement keeps returning 'None' for empty list
<p>I'm a beginner at coding in Python and I've been practising with exercises CodeWars.</p>

<p>There's this exercise which basically wants you to recreate the display function of the "likes" on Facebook, i.e. how it shows the number likes you have on a post etc.</p>

<p>Here is my code:</p>

<pre><code>def likes(names):
    for name in names:
        if len(names) == 0:
           return 'no one likes this'
        elif len(names) == 1:
           return '%s likes this' % (name)
        elif len(names) == 2:
           return '%s and %s like this' % (names[0], names[1])
        elif len(names) == 3:
           return '%s, %s and %s like this' % (names[0], names[1], names[2])
        elif len(names) &gt;= 4:
           return '%s, %s and %s others like this' % (names[0], names[1], len(names) - 2)

print likes([])
print likes(['Peter'])
print likes(['Alex', 'Jacob', 'Mark', 'Max'])
</code></pre>

<p>This prints out:</p>

<pre><code>None
Peter likes this
Alex, Jacob and 2 others like this
</code></pre>

<p>My main issue here is that my first 'if' statement is not producing the string 'no one likes this' for when the argument: [] is empty. Is there a way around this problem?</p>

<p>If <code>names</code> is an empty list the <code>for</code> loop won't be executed at all, which will cause the function to return <code>None</code>. You should change the structure of your function (hint: you might not even need a loop, not an explicit one at least). There is no point in having a loop and then <code>return</code> on the very first iteration.</p>

python list function if-statement