Python | Check order specific data type in tuple

Sometimes, while working with records, we can have a problem in which we need to test for correct ordering of data types inserted while filling forms etc. at the backend. These tests are generally handled at frontend while web development, but are recommended to be tested at backend as well. For this, we sometimes, need to check for record’s data types according to their ordering. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using chained if and isinstance()
This task can be performed using combination of above functionalities. In this, we just need to test for the data type using the isintance(), and to check each element of tuple we employ chained if statements.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Check order specific data type in tuple
# Using chained if and isinstance()
  
# Initializing tuple
test_tup = ('gfg', ['is', 'best'], 1)
  
# printing original tuple
print("The original tuple is : " + str(test_tup))
  
# Check order specific data type in tuple
# Using chained if and isinstance()
res = isinstance(test_tup, tuple)\
      and isinstance(test_tup[0], str)\
      and isinstance(test_tup[1], list)\
      and isinstance(test_tup[2], int)
  
# printing result
print("Does all the instances match required data types in order ? : " + str(res))

chevron_right


Output :



The original tuple is : ('gfg', ['is', 'best'], 1)
Does all the instances match required data types in order ? : True

 

Method #2 : Using map() + type() + isinstance()
The combination of above functions can also be used to achieve this task. The checking for types for each element in tuple is extended by map(). The advantage of this method is that it allows us to define the data type ordering beforehand as a list.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Check order specific data type in tuple
# Using map() + type() + isinstance()
  
# Initializing tuple
test_tup = ('gfg', ['is', 'best'], 1)
  
# printing original tuple
print("The original tuple is : " + str(test_tup))
  
# data type order list 
data_list = [str, list, int]
  
# Check order specific data type in tuple
# Using map() + type() + isinstance()
res = isinstance(test_tup, tuple) and list(map(type, test_tup)) == data_list
  
# printing result
print("Does all the instances match required data types in order ? : " + str(res))

chevron_right


Output :

The original tuple is : ('gfg', ['is', 'best'], 1)
Does all the instances match required data types in order ? : True


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.