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
# Python3 code to demonstrate working of
# Get tuple element data types
# Using map() + type()
# Initializing tuple
test_tup = ('gfg', 1, ['is', 'best'])
# printing original tuple
print("The original tuple is : " + str(test_tup))
# Get tuple element data types
# Using map() + type()
res = list(map(type, test_tup))
# printing result
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
# Python3 code to demonstrate working of
# Get tuple element data types
# Using collections.Sequence + isinstance() + type()
import collections
# Initializing tuple
test_tup = ('gfg', 1, ['is', 'best'])
# printing original tuple
print("The original tuple is : " + str(test_tup))
# Get tuple element data types
# Using collections.Sequence + isinstance() + type()
res = [(type(ele), len(ele) if isinstance(ele, collections.Sequence) else None)
for ele in test_tup]
# printing result
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
# Python3 code to demonstrate working of
# Get tuple element data types
# Using list comprehension and type()
# Initializing tuple
test_tup = ('gfg', 1, ['is', 'best'])
# printing original tuple
print("The original tuple is : " + str(test_tup))
# Get tuple element data types
# Using list comprehension and type()
res = [type(ele) for ele in test_tup]
# printing result
print("The data types of tuple in order are : " + str(res))
# This code is contributed by Edula Vinay Kumar Reddy
OutputThe 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
# Initializing tuple
test_tup = ('gfg', 1, ['is', 'best'])
# printing original tuple
print("The original tuple is : " + str(test_tup))
# Get tuple element data types
# Using loop
res = []
for ele in test_tup:
res.append(type(ele))
# printing result
print("The data types of tuple in order are : " + str(res))
OutputThe 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
# Initializing tuple
test_tup = ('gfg', 1, ['is', 'best'])
# printing original tuple
print("The original tuple is : " + str(test_tup))
# Get tuple element data types
# Using map() and lambda function
res = list(map(lambda x: type(x), test_tup))
# printing result
print("The data types of tuple in order are : " + str(res))
OutputThe 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.
Similar Reads
Python - Remove particular data type Elements from Tuple
Sometimes, while working with Python tuples, we can have a problem in which we need to remove particular data type elements from tuple. This kind of problem can occur in domains which require data preprocessing. Let's discuss certain ways in which this task can be performed. Input : test_tuple = (4,
6 min read
Python - Alternate Elements operation on Tuple
Sometimes, while working with Python Tuples, we can have problem in which we need to perform operations of extracted alternate chains of tuples. This kind of operation can have application in many domains such as web development. Lets discuss certain ways in which this task can be performed. Input :
5 min read
Python - Flatten Nested Tuples
Sometimes, while working with Python Tuples, we can have a problem in which we need to perform flattening of tuples, which can be nested and undesired. This can have application across many domains such as Data Science and web development. Let's discuss certain way in which this task can be performe
7 min read
Python - Test Similar Data Type in Tuple
Sometimes, while working with Python tuples, we can have a problem in which we need to test if all the elements in the tuple have the same data type. This kind of problem can have applications across all domains such as web development and day-day programming. Let's discuss certain ways in which thi
6 min read
Python Tuple - len() Method
While working with tuples many times we need to find the length of the tuple, and, instead of counting the number of elements with loops, we can also use Python len(). We will learn about the len() method used for tuples in Python. Example: Python3 Tuple =( 1, 0, 4, 2, 5, 6, 7, 5) print(len(Tuple))
2 min read
Python | Check if element is present in tuple of tuples
Sometimes the data that we use is in the form of tuples and often we need to look into the nested tuples as well. The common problem that this can solve is looking for missing data or na value in data preprocessing. Let's discuss certain ways in which this can be performed. Method #1: Using any() an
4 min read
Python - Clearing a tuple
Sometimes, while working with Records data, we can have a problem in which we may require to perform clearing of data records. Tuples, being immutable cannot be modified and hence makes this job tough. Let's discuss certain ways in which this task can be performed. Method #1 : Using list() + clear()
4 min read
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 the 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
8 min read
Flatten tuple of List to tuple - Python
The task of flattening a tuple of lists to a tuple in Python involves extracting and combining elements from multiple lists within a tuple into a single flattened tuple. For example, given tup = ([5, 6], [6, 7, 8, 9], [3]), the goal is to flatten it into (5, 6, 6, 7, 8, 9, 3). Using itertools.chain(
3 min read
Python - Get Even indexed elements in Tuple
Sometimes, while working with Python data, one can have a problem in which we need to perform the task of extracting just even indexed elements in tuples. This kind of problem is quite common and can have possible application in many domains such as day-day programming. Let's discuss certain ways in
5 min read