DEV Community

Cover image for How to Check if a List is Empty in Python

How to Check if a List is Empty in Python

Jeremy Grifski on February 21, 2019

In an effort to start cross-posting, I figured I'd share another brief Python article with everyone. Today, we’re going to learn how to check if a ...
Collapse
 
vorsprung profile image
vorsprung

Or you could say

if my_list == []:

I've never understood the one way is more pythonic than another thing

Collapse
 
renegadecoder94 profile image
Jeremy Grifski • Edited

That's another good one! I'll add it to the list.

The term Pythonic is a little ambiguous to be honest, but I like to think that it sums up one of the core ideas of Python which is "there is only one way to do it." In other words, the Python developers try to avoid including unnecessary complexity in the language by enforcing a certain problem solving style. As a result, you won't find switch statements or c-style for loops in Python.

As a bonus, choosing the Pythonic solution (i.e. testing emptiness by type flexibility) is usually the cleanest and most optimized solution. Of course, that comes with a caveat: Pythonic solutions are not always readable solutions.