Python | Get tuple element data types
Tuples can be a collection of various data types, and unlike simpler data types, conventional methods of getting the type of each element of tuple is not possible. For this we need to have different ways to achieve this task. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using
map() + type()
Using this function is most conventional and best way to perform this task. In this, we just allow
map() to extend the logic of finding data types using
type() to each element of tuple.
The original tuple is : ('gfg', 1, ['is', 'best']) The data types of tuple in order are : [<class 'str'>, <class 'int'>, <class 'list'>]
Method #2 : Using collections.Sequence +
isinstance() + type()
We can perform this task using the combination of above functions. The additional advantage of using this method it that it also provides us with the length of each element if its type is complex data type.
The original tuple is : ('gfg', 1, ['is', 'best']) The data types of tuple in order are : [(<class 'str'>, 3), (<class 'int'>, None), (<class 'list'>, 2)]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course