12

In one coding example i saw the following code snippet that returns True if the list is empty and False if not

return a == []

the reason for that is to avoid writing

if a:
    return False
else:
    return True

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

9
  • 1
    x == [] seems to short circuit (CPython 3), so the performance of both approaches should be O(1), and multiple thousands of entries vs. one or two entries should not matter. Commented Sep 11, 2016 at 6:42
  • 1
    That second snippet doesn't match your described logic, I think True and False are switched around. Your intent could just be written not bool(a) by the way. Commented Sep 11, 2016 at 6:42
  • 4
    @detly or even not a Commented Sep 11, 2016 at 6:42
  • 3
    Never mind the speed, which is the easiest to understand and maintain? Commented Sep 11, 2016 at 6:42
  • 1
    Use Python's truthiness, where [] tests as false, and non-empty lists test as true. No need to get fancy because the whole point of this truthiness approach is to be able to use list lengths equivalent to boolean values. Commented Sep 11, 2016 at 6:43

2 Answers 2

14

No. There is no speed difference in either case. Since in both cases the length of the list is checked first. In the first case, the len of a is compared with the len of [] before any further comparison. Most times the len should differ, so the test just returns immediately.

But the more pythonic way would be to just return not a or convert it using bool and then return it:

return not a

# or 

return not bool(a)
Sign up to request clarification or add additional context in comments.

Comments

2

If you're asking which method would faster if put in a function(hence the return's), then I used the timeit 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:

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))

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:

method one speed(milliseconds): 0.2571859563796641
-----------------------------   ------------------
method two speed(milliseconds): 0.2679253742685615

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

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: Readability counts.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.