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 | Sort tuple list by Nth element of tuple
- Python | Replace tuple according to Nth tuple element
- Python | Convert mixed data types tuple list to string list
- Python - Raise elements of tuple as power to another tuple
- Python - Convert Tuple String to Integer Tuple
- Python - Convert Tuple to Tuple Pair
- Python - Flatten tuple of List to tuple
- Python - Convert Tuple Matrix to Tuple List
- Python | Get first index values in tuple of strings
- Python | Get all tuple keys from dictionary
- Python | How to get unique elements in nested tuple
- Python - Get minimum difference in Tuple pair
- Python - Get maximum of Nth column from tuple list
- Python - Get Nth column elements in Tuple Strings
- Python - Get Even indexed elements in Tuple
- Python | Check order specific data type in tuple
- Python | Intersection in Tuple Records Data
- Python - Test Similar Data Type in Tuple
- Python - Remove particular data type Elements from Tuple
- Python | Count the elements in a list until an element is a Tuple
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.