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 - 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 | 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
Tuple within a Tuple in Python In the realm of Python programming, tuples offer a flexible solution for organizing data effectively. By allowing the inclusion of other tuples within them, tuples further enhance their capabilities. In this article, we will see how we can use tuple within a tuple in Python. Using Tuple within a Tup
4 min read
Python - Test if all elements in list are of same type When working with lists in Python, there are times when we need to ensure that all the elements in the list are of the same type or not. This is particularly useful in tasks like data analysis where uniformity is required for calculations. In this article, we will check several methods to perform th
2 min read
Access front and rear element of Python tuple Sometimes, while working with records, we can have a problem in which we need to access the initial and last data of a particular record. This kind of problem can have application in many domains. Let's discuss some ways in which this problem can be solved. Method #1: Using Access Brackets We can pe
6 min read
Python - Filter Tuples with Integers Given Tuple list, filter tuples which are having just int data type. Input : [(4, 5, "GFg"), (3, ), ("Gfg", )] Output : [(3, )] Explanation : 1 tuple (3, ) with all integral values. Input : [(4, 5, "GFg"), (3, "Best" ), ("Gfg", )] Output : [] Explanation : No tuple with all integers. Method #1 : Usi
5 min read