What do you think of when you hear “an empty list?” Your first thought would be a list with zero elements. Its length is 0. It still consumes memory because of the underlying object structure and memory allocation strategies.
Declaring an empty list
The most optimal way to initialize an empty list is to use square brackets ([ ]) with nothing in them, and assign it to a variable, like main_list = []. Why is it efficient? Because it does not require any function calling. So, no overhead.
empty_list = [] print(empty_list) # Output: [] print(type(empty_list)) # <class 'list'>
Alternative: list() Constructor
Another way is the list() constructor, which also creates an empty list. However, it is slightly lower than [] because it requires a global lookup and function call.
another_empty_list = list() print(another_empty_list) # Output: [] print(type(another_empty_list)) # Output: <class 'list'>
Pre-allocating list size
Lists do not require preallocating, and in some cases, it is not considered the best approach. However, if you want, you can initialize with placeholders like None, if needed:
preallocated_list = [None] * 1000 # Output: A list with 1000 None elements like this [None, None,.....None]
Checking if a list is empty
The most efficient way to check if a list is empty is to use the implicit boolean check using the not operator with an if statement. If a list is empty, it returns False, so using the not operator, it returns True.
This leverages Python’s truth value testing. The time and space complexity for checking emptiness is O(1) because it is a direct evaluation of its internal state, which is a constant time.
empty_list = [] if not empty_list: print("List is empty") # Output: List is empty
Alternative 1: Comparing length with 0
There is a built-in len() function to check the list’s length, and if it is 0, it is empty. Since it requires a function call, it is marginally slower than an “if not” expression.
empty_list = [] if len(empty_list) == 0: print("List is empty") # Output: List is empty
Alternative 2: Comparing our list to [] (Not Recommended)
Using the == operator, you can compare two variables and check if they are the same. For example, we can check empty_list == []. Logically, it works and returns correct output, but it is not an efficient approach because it creates a new empty list for comparison, which creates an overhead where performance is critical.
Avoid the “is” operator for comparison
Another problematic approach is using “is” operator to check for emptiness because it checks object identity, not content.
empty_list = [] if empty_list is []: print("List is empty") else: print("List is not empty") # Output: List is not empty
You can see from the above code and its output that even if the list is empty, we get the output that says, “List is not empty”. Why? because it does not check the list’s content. Instead, it compares the two list objects, which are fundamentally different.
Type-specific checks
While checking if you are unsure of the object you are working with, you can use the built-in isinstance() method like this:
empty_list = [] if isinstance(empty_list, list) and not empty_list: print("An empty list.") else: print("Not an empty list.") # Output: An empty list.
That’s all!