How to Create an Empty Tuple in Python?
Last Updated :
06 Nov, 2024
Improve
Like an empty list, we can create an empty tuple in Python. Empty tuples can work as a placeholder and are more memory efficient compared to empty lists because tuples are immutable.
Using Empty Parentheses
Simplest way to create an empty tuple is by using ( ).
# Creating an empty tuple using parentheses
t = ()
print(t)
print(type(t))
print(len(t))
Output
() <class 'tuple'> 0
Using the tuple()
Constructor
We can also use tuple() constructor to create empty tuple. This method is used when tuple is created dynamically.
# Creating an empty tuple using the tuple() constructor
t = tuple()
print(t)
print(type(t))
print(len(t))
Output
() <class 'tuple'> 0
Checking if a Tuple is Empty
We can verify is tuple is empty by checking length. Another method is to compare given tuple with an empty tuple.
# Checking if a tuple is empty
t = ()
if len(t) == 0:
print("The tuple is empty.")
# Alternative way using direct comparison
if t == ():
print("The tuple is empty.")
Output
The tuple is empty. The tuple is empty.
Can we add items to an empty tuple?
No, you cannot add items to an empty tuple (or any tuple) in Python directly because tuples are immutable. Once a tuple is created, its items cannot be changed, added, or removed.
Though, you can create a new tuple with additional elements.
# Creating a new tuple by adding elements
t = () # empty tuple
t1 = t + (1, 2, 3) # creates a new tuple with additional items
print(t1)
Output
(1, 2, 3)