Python | Get tuple element data types
Last Updated :
10 May, 2023
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.
Python3
test_tup = ( 'gfg' , 1 , [ 'is' , 'best' ])
print ( "The original tuple is : " + str (test_tup))
res = list ( map ( type , test_tup))
print ( "The data types of tuple in order are : " + str (res))
|
Output :
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.
Python3
import collections
test_tup = ( 'gfg' , 1 , [ 'is' , 'best' ])
print ( "The original tuple is : " + str (test_tup))
res = [( type (ele), len (ele) if isinstance (ele, collections.Sequence) else None )
for ele in test_tup]
print ( "The data types of tuple in order are : " + str (res))
|
Output :
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)]
Method #3: Using list comprehension and type()
This method uses a list comprehension to iterate through each element in the tuple and get its data type using the type() function
Python3
test_tup = ( 'gfg' , 1 , [ 'is' , 'best' ])
print ( "The original tuple is : " + str (test_tup))
res = [ type (ele) for ele in test_tup]
print ( "The data types of tuple in order are : " + str (res))
|
Output
The original tuple is : ('gfg', 1, ['is', 'best'])
The data types of tuple in order are : [<class 'str'>, <class 'int'>, <class 'list'>]
Time complexity: O(n)
Auxiliary Space: O(n)
Method #4 : Using loop
Approach:
Step 1: Initialize the tuple
Step 2: Create an empty list to store the data types
Step 3: Loop through each element in the tuple
Step 4: Get the data type of the element using the type() function
Step 5: Append the data type to the list
Step 6: Print the result
Python3
test_tup = ( 'gfg' , 1 , [ 'is' , 'best' ])
print ( "The original tuple is : " + str (test_tup))
res = []
for ele in test_tup:
res.append( type (ele))
print ( "The data types of tuple in order are : " + str (res))
|
Output
The original tuple is : ('gfg', 1, ['is', 'best'])
The data types of tuple in order are : [<class 'str'>, <class 'int'>, <class 'list'>]
Time complexity: O(n), where n is the number of elements in the tuple
Auxiliary space: O(n), where n is the number of elements in the tuple (used to store the result in a list)
Method 5 : Using the map() function and lambda function
Step-by-step approach:
- First, we initialize the tuple test_tup.
- We then use the map() function with a lambda function to apply the type() function to each element of the tuple.
- The lambda function takes an element x and returns its type by calling the type() function on it.
- The map() function applies the lambda function to each element of the tuple and returns a map object.
- We convert the map object to a list using the list() function and store it in res.
- Finally, we print the result.
Python3
test_tup = ( 'gfg' , 1 , [ 'is' , 'best' ])
print ( "The original tuple is : " + str (test_tup))
res = list ( map ( lambda x: type (x), test_tup))
print ( "The data types of tuple in order are : " + str (res))
|
Output
The original tuple is : ('gfg', 1, ['is', 'best'])
The data types of tuple in order are : [<class 'str'>, <class 'int'>, <class 'list'>]
Time Complexity: O(n), where n is the length of the tuple.
Auxiliary Space: O(n), where n is the length of the tuple.
Share your thoughts in the comments
Please Login to comment...