Skip to content
Related Articles

Related Articles

Python – Find frequency of given Datatype in tuple
  • Last Updated : 03 Jul, 2020

Sometimes, while working with Python records, we can have a problem in which we need to extract count of any data type occurred in tuple. This can have application in various domains such as day-day programming and web development. Let’s discuss certain ways in which this task can be performed.

Input : test_tuple = (5, ‘Gfg’, 2, 8.8, 1.2, ‘is’), data_type = int
Output : 2

Input : test_tuple = (5, ‘Gfg’, 2, 8.8, 1.2, ‘is’), data_type = str
Output : 2

Method #1 : Using loop + isinstance()
The combination of above functions can be used to solve this problem. In this, we perform the task of checking for data type using isinstance() and run a counter to increment on match.




# Python3 code to demonstrate working of 
# Data type frequency in tuple
# Using loop + isinstance()
  
# initializing tuples
test_tuple = (5, 'Gfg', 2, 8.8, 1.2, 'is')
  
# printing original tuple
print("The original tuple : " + str(test_tuple))
  
# initializing data type
data_type = float
  
# Data type frequency in tuple
# Using loop + isinstance()
count = 0
for ele in test_tuple:
    if isinstance(ele, float):
        count = count + 1
  
# printing result 
print("The data type frequency : " + str(count))
Output :



The original tuple : (5, 'Gfg', 2, 8.8, 1.2, 'is')
The data type frequency : 2

 

Method #2 : Using sum() + isinstance()
The combination of above functions can also be used to solve this problem. This used similar way of solving as above method, just in shorthand way using sum() for counting.




# Python3 code to demonstrate working of 
# Data type frequency in tuple
# Using sum() + isinstance()
  
# initializing tuples
test_tuple = (5, 'Gfg', 2, 8.8, 1.2, 'is')
  
# printing original tuple
print("The original tuple : " + str(test_tuple))
  
# initializing data type
data_type = float
  
# Data type frequency in tuple
# Using sum() + isinstance()
count = sum(1 for ele in test_tuple if isinstance(ele, data_type))
  
# printing result 
print("The data type frequency : " + str(count))
Output :
The original tuple : (5, 'Gfg', 2, 8.8, 1.2, 'is')
The data type frequency : 2

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :