Get tuple element data types in Python


When it is required to get the tuple element of data types, the 'map' method and the 'type' method can be used.

The map function applies a given function/operation to every item in an iterable (such as list, tuple). It returns a list as the result.

The 'type' method returns the type of class of the argument that is passed to it.

A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on).

Below is a demonstration of the same −

Example

Live Demo

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

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

my_result = list(map(type, my_tuple))

print("The data types of tuple in order are : ")
print(my_result)

Output

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

Explanation

  • A tuple that contains list is defined, and displayed on the console.
  • The map method is used to apply the 'type' method to all elements of the tuple of list.
  • This is converted to a list.
  • This is assigned to a value.
  • It is displayed on the console.

Updated on: 12-Mar-2021

683 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements