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)]
- Python | Convert mixed data types tuple list to string list
- Python | Sort tuple list by Nth element of tuple
- Python | Replace tuple according to Nth tuple element
- Python | Intersection in Tuple Records Data
- Python | Counting Nth tuple element
- Python | Adding N to Kth tuple element
- Python | Sort a tuple by its float element
- Python | Minimum element in tuple list
- Python | Update each element in tuple list
- Python | Remove particular element from tuple list
- Python | Check if element is present in tuple
- Python | Count occurrences of an element in a Tuple
- Python | Maximum element in tuple list
- Python | Check order specific data type in tuple
- Python | Check if element is present in tuple of tuples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.