Get tuple element data types in Python

When you need to determine the data types of elements in a tuple, you can use Python's map() function combined with the type() function.

The map() function applies a given function to every item in an iterable (such as list, tuple) and returns an iterator. The type() function returns the class type of any object passed to it.

Basic Example

Here's how to get data types of tuple elements ?

my_tuple = ('Hi', 23, ['there', 'Will'])

print("The tuple is:")
print(my_tuple)

# Apply type() to each element using map()
data_types = list(map(type, my_tuple))

print("The data types of tuple elements are:")
print(data_types)
The tuple is:
('Hi', 23, ['there', 'Will'])
The data types of tuple elements are:
[<class 'str'>, <class 'int'>, <class 'list'>]

Getting Type Names Only

To get just the type names instead of full class representations ?

my_tuple = (3.14, True, "Hello", [1, 2, 3], (4, 5))

print("The tuple is:")
print(my_tuple)

# Get type names using __name__ attribute
type_names = [type(element).__name__ for element in my_tuple]

print("Type names:")
print(type_names)
The tuple is:
(3.14, True, 'Hello', [1, 2, 3], (4, 5))
Type names:
['float', 'bool', 'str', 'list', 'tuple']

Comparison of Methods

Method Output Format Best For
map(type, tuple) Full class objects Detailed type information
List comprehension with __name__ Simple type names Clean readable output

Practical Use Case

This technique is useful for data validation and debugging ?

def validate_tuple_types(data, expected_types):
    actual_types = [type(item).__name__ for item in data]
    
    print(f"Data: {data}")
    print(f"Expected: {expected_types}")
    print(f"Actual: {actual_types}")
    
    return actual_types == expected_types

# Test with mixed data
test_data = ("John", 25, 85.5, True)
expected = ["str", "int", "float", "bool"]

is_valid = validate_tuple_types(test_data, expected)
print(f"Valid format: {is_valid}")
Data: ('John', 25, 85.5, True)
Expected: ['str', 'int', 'float', 'bool']
Actual: ['str', 'int', 'float', 'bool']
Valid format: True

Conclusion

Use map(type, tuple) to get detailed type information of tuple elements. For cleaner output, use list comprehension with type().__name__ to get simple type names.

Updated on: 2026-03-25T17:30:04+05:30

913 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements