Python return False if list is empty
<p>In one coding example i saw the following code snippet that returns <strong>True</strong> if the list is empty and <strong>False</strong> if not</p>

<pre><code>return a == []
</code></pre>

<p>the reason for that is to avoid writing</p>

<pre><code>if a:
    return False
else:
    return True
</code></pre>

<p>In a real example with multiple thousands of entries, is there any speed difference i should be aware of?</p>

<p>If you're asking which method would faster if put in a function(hence the <code>return</code>'s), then I used the <code>timeit</code> module to do a little testing. I put each method in a function, and then ran the program to see which function ran faster. Here is the program:</p>

<pre><code>import timeit

def is_empty2():
    a = []
    if a:
        return True
    else:
        return False

def is_empty1():
    a = []
    return a == []


print("Time for method 2:")
print(timeit.timeit(is_empty2))
print("")
print("Time for method 1:")
print(timeit.timeit(is_empty1))
</code></pre>

<p>I ran the program five times, each time recording the speed for each function. After getting an average for each time, here is what I came up with:</p>

<pre><code>method one speed(milliseconds): 0.2571859563796641
-----------------------------   ------------------
method two speed(milliseconds): 0.2679253742685615
</code></pre>

<p>At least from my testing above, the first method you described in your question was <em>slightly</em> faster than the second method. Of course those numbers above could drastically change depending on what exactly is inside those two functions.</p>

<p>I agree however, with what Cdarke said in the comments. Go with the one that is the most clear and concise. Don't go with one option solely based upon its speed. in the words of Guido van Rosom: <em>Readability counts</em>. </p>

python