You are currently viewing Python Empty Tuple with Examples

An empty tuple in Python is a tuple that contains no elements. Tuple is represented by a pair of parentheses with nothing inside, like this, () or the built-in function tuple(). You can check whether a tuple is empty or not by finding its length using the built-in len() function.

Advertisements

If the length of the tuple is zero, it means that the tuple is empty. You can also use the not operator to check if a tuple is empty or not. In this article, I will explain empty tuples in python using all these methods with examples.

1. Quick Examples of Empty Tuple

If you are in a hurry, below are some quick examples of empty tuples in python.


# Quick examples of empty tuple

# Example 1: Using empty parentheses
mytuple = ()

# Example 2: Using tuple() function
mytuple = tuple()

# Example 3: Compare the two tuples
tuples1 = ()
tuples2 = ()
result = tuples1 == tuples2

# Example 4: Comparing the tuples  
tuples1 = () 
tuples2 = ('Spark', 'Python', 'Hadoop')  
  if tuples1 == tuples2:  
    print("The tuple is empty")
else:
    print("The tuple is not empty")

# Example 5: Check if a tuple is empty 
tuples = ()
if len(tuples) == 0:
    print("The tuple is empty")
else:
    print("The tuple is not empty")

# Example 6: Using the not operator
tuples = ()
if not tuples:
    print("The tuple is empty")
else:
    print("The tuple is not empty")

# Example 7: Changing a tuple to an empty tuple
tuples = ('Spark','Python','Pandas','Pyspark','Java')
tuples = tuples[:2] + tuples[4:]  
List = list(tuples)   
# Creating a for loop 
for i in range(len(List)):  
  List.pop()  
# Converting the list back to a tuple  
tuples = tuple(List)  

2. Create an Empty Tuple

You can create an empty tuple in Python using empty parentheses () or the built-in function tuple() without any arguments. Here’s an example of creating an empty tuple using each method. Both of these methods will create an empty tuple object that can be assigned to a variable.


# Using empty parentheses
mytuple = ()

# Using tuple() function
mytuple = tuple()

3. Comparing with Another Empty Tuple

You can compare two empty tuples, the result will be True because both tuples have no elements. In the below example, the comparison tuples1==tuples2 returns True, because both tuples have no elements.


# Create two empty tuples
tuples1 = ()
tuples2 = ()

# Compare the two tuples
result = tuples1 == tuples2
print(result) 

# Output:
# True

Similarly, Let’s compare the two tuples tuples1 and tuples2 using if statemnt. If tuples1 is equal to tuples2, then it prints “The tuple is empty“, otherwise it prints “The tuple is not empty“. For example,


# Creating an empty tuple 
tuples1 = () 
  
# Creating a second tuple  
tuples2 = ('Spark', 'Python', 'Hadoop')  
  
# Comparing the tuples  
if tuples1 == tuples2:  
    print("The tuple is empty")
else:
    print("The tuple is not empty")
    
# Output
# The tuple is not empty

3. Check if Tuple is Empty

You can check if a tuple is empty by using the built-in len() function, which returns the length of a tuple. For example, you first define an empty tuple tuples using empty parentheses (). Then use the len() function to check the length of the tuple.


# Check if a tuple is empty 
tuples = ()
if len(tuples) == 0:
    print("The tuple is empty")
else:
    print("The tuple is not empty")

# Output:
# The tuple is empty

If the length of the tuple is 0, then the tuple is empty and you print a message indicating that. Otherwise, you print a message indicating that the tuple is not empty.

4. Using the not Operator

You can also use a boolean evaluation of the tuple directly to check if it’s empty. For example, not tuples statement will evaluate  True if the tuple is empty, or False otherwise. This is because an empty tuple evaluates to False in a boolean context.


# Creating an empty tuple 
tuples = ()

# Using the not operator
if not tuples:
    print("The tuple is empty")
else:
    print("The tuple is not empty")

# Output
# The tuple is empty

5. Change a Tuple to Empty Tuple

You can change a tuple into an empty tuple, In order to do this you have to convert tuple to list and use the pop() to remove the lements and convert it back to tuple.

In realtime, you may not use this approach as it is not that efficient.


# Changing a tuple to empty tuple
tuples = ('Spark','Python','Pandas','Pyspark','Java')
print("Original tuple: ",tuples)  

# Converting our tuple into a Python List   
List = list(tuples)   
  
# Creating a for loop 
# Delete all the elements of the list  
for i in range(len(List)):  
  List.pop()  
  
# Converting the list back to a tuple  
tuples = tuple(List)  
print("New empty tuple: ", tuples) 

# Output
# Original tuple:  ('Spark', 'Python', 'Pandas', 'Pyspark', 'Java')
# New empty tuple:  ()

Here, first, you define a tuple called tuples containing five elements, and convert the tuple into a list using the list() function. After that, you create a for loop that iterates through the length of the list and removes each element using the pop() method. Finally, you convert the modified list back into a tuple using the tuple() function, which creates a new empty tuple.

Conclusion

In this article, I have explained empty tuples in Python by using, parentheses (), tuple(), and, len() functions with examples.

Happy Learning !!

Related Articles

References