An empty tuple means a tuple without any elements. For example, you can call this “()” an empty tuple. Tuples are written in parentheses; if there are no elements in it, it is just a pair of parentheses with nothing inside.
Creating an empty tuple
The most optimal and fastest approach to create an empty tuple is to use the literal () syntax.
# Creating an empty tuple empty_tuple = () print(empty_tuple) # Output: () print(type(empty_tuple)) # Output: <class 'tuple'>
It has a widely recognized syntax and minimal overhead.
Using tuple() constructor
Another way is using the tuple() constructor without any arguments.
# Using the tuple() Constructor tuple_constructor = tuple() print(tuple_constructor) # Output: ()
It is slightly less efficient than “()” because it calls the function, which adds an extra overhead.
However, it is helpful when you have to create a tuple dynamically (e.g., tuple(iterable)), but it is less efficient for empty tuples.
Tuple of empty tuples
The efficient, fastest, and most Pythonic way to create a tuple of empty tuples is to use tuple literals with a trailing comma like this: ((),).
# Create a tuple of an empty tuples empty_tuples_in_tuple = ((),) print(empty_tuples_in_tuple) # Output: ((),)
The above code shows the trailing comma, which explicitly makes it a tuple of one element (an empty tuple).
You can use it as a placeholder in a nested data structure like this:
nested = {"key": ((), "default")} print(nested["key"]) # Output: ((),)
Tuple of length n with empty-like placeholders (None)
If you want to create pre-defined length empty-like tuples, use the (None,) * n expression where n is a tuple of length.
# Creating a Tuple of Length n with empty-like placeholders: n = 6 empty_placeholders = (None,) * n print(empty_placeholders) # Output: (None, None, None, None, None, None)
It is helpful in pre-allocating a fixed-size tuple.
You can also use empty tuples as place holders like this:
n = 3 tuple_of_empty_tuples = ((),) * n print(tuple_of_empty_tuples) # Output: ((), (), ())
How to check if a tuple is empty
The most efficient way to check if a tuple is empty is to use an implicit boolean check using the not operator with an if statement.
If the tuple is empty, the not operator returns True, meaning it is in fact empty. If it returns False, it is not empty.
empty_tuple = () # Implicit Boolean Check if not empty_tuple: print("The tuple is empty") # Output: The tuple is empty
Since there are no function calls, the time complexity is O(1), which is the fastest. It leverages Python’s __bool__ (or __len__ for sequences) under the hood.
Explicit length check
Another way is to check the length of the tuple. If it is 0, it is empty; otherwise, it is not. You can check the tuple’s length using the len() function.
empty_tuple = () # Explicit Boolean Check empty_tuple = () if len(empty_tuple) == 0: print("Tuple is empty") # Output: Tuple is empty
It is a more readable approach, and here, the time complexity is O(1), too, but “not operator” is marginally faster.
Direct comparison
The direct comparison approach is simple and readable because you just need to compare your input tuple with () using the “==” operator like this: if your_tuple == (). However, it is the slowest approach because it creates a temporary tuple, which takes a minor time.
your_tuple = () if your_tuple == (): print("Tuple is empty") # Output: Tuple is empty
Checking if a tuple contains an empty tuple
If you want to verify whether a tuple contains an empty tuple, you can use the “in” membership operator.
# Checking If a Tuple Contains an Empty Tuple main_tuple = ((), "hello", 123) if () in main_tuple: print("Contains an empty tuple") # Output: Contains an empty tuple
That’s all!