Tuple Unpacking Check

Tuple unpacking can be used to check if a variable is a tuple. If the variable is a tuple, then the unpacking will succeed, otherwise, it will raise a TypeError.

Steps:

  1. Use try-except block to catch TypeError raised by tuple unpacking.
  2. If the unpacking succeeds, then the variable is a tuple.

Python3




def is_tuple(my_var):
    try:
        a, b, c = my_var
        return True
    except TypeError:
        return False
 
 
# Example usage
t = (1, 2, 3)
l = [1, 2, 3]
s = '123'
 
print(is_tuple(t))
print(is_tuple(l))
print(is_tuple(s))


Output

True
True
True

Time Complexity: The time complexity of this approach is O(1)
Auxiliary Space: The auxiliary space used in this approach is O(1)

Python | Check if variable is tuple

Sometimes, while working with Python, we can have a problem in which we need to check if a variable is single or a record. This has applications in domains in which we need to restrict the type of data we work on. Let’s discuss certain ways in which this task can be performed. 

Method #1: Using type() This inbuilt function can be used as shorthand to perform this task. It checks for the type of variable and can be employed to check tuple as well. 

Python3




# Python3 code to demonstrate working of
# Check if variable is tuple
# using type()
 
# initialize tuple
test_tup = (4, 5, 6)
 
# printing original tuple
print("The original tuple : " + str(test_tup))
 
# Check if variable is tuple
# using type()
res = type(test_tup) is tuple
 
# printing result
print("Is variable tuple ? : " + str(res))


Output

The original tuple : (4, 5, 6)
Is variable tuple ? : True

The time complexity is constant time or O(1).

The auxiliary space is also constant or O(1). 

Method #2: Using instance() 

Yet another function that can be employed to perform this task. It also returns true, in case the parent class of variable(if exists) is a tuple. 

Python3




# Python3 code to demonstrate working of
# Check if variable is tuple
# using isinstance()
 
# initialize tuple
test_tup = (4, 5, 6)
 
# printing original tuple
print("The original tuple : " + str(test_tup))
 
# Check if variable is tuple
# using isinstance()
res = isinstance(test_tup, tuple)
 
# printing result
print("Is variable tuple ? : " + str(res))


Output

The original tuple : (4, 5, 6)
Is variable tuple ? : True

The time complexity is O(1)

The auxiliary space is also O(1)

Method 3: Using list comprehension 

Python3




test_tup = (4, 5, 6)
x=["true" if type(test_tup)==tuple else "false"]
print(x)


Output

['true']

Method 4: Using __class__
 

Python3




#initialize tuple
test_tup = (4, 5, 6)
 
#printing original tuple
print("The original tuple : " + str(test_tup))
 
#Check if variable is tuple
# class
res = test_tup.__class__==tuple
 
#printing result
print("Is variable tuple ? : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original tuple : (4, 5, 6)
Is variable tuple ? : True

Time Complexity: O(1)
Auxiliary Space: O(1)
Explanation:
In this approach, the class attribute of the variable is used to check if it belongs to the tuple class.
If the class attribute is equal to the tuple class, it returns True, otherwise, it returns False.
This approach is simple and efficient with a time complexity of O(1).

Similar Reads

Method 5: “indexing and length check”

...

Method 6: “Iterative Check for Tuple”

...

Approach: element access or subscripting

...

Approach: Tuple Unpacking Check

...

Duck Typing Check

This approach tries to access the first element of the variable (i.e., ‘var[0]’) and also checks if the variable has a defined length (i.e.,’ len(var)’). If both of these operations can be performed without raising a ‘TypeError’ or ‘IndexError’, then the variable is assumed to be a tuple and the function returns ‘True’. If either of these operations raises an error, then the variable is assumed not to be a tuple and the function returns ‘False’....