Python - Find frequency of given Datatype in tuple
Last Updated :
16 May, 2023
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
# 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
# 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
Method #3 : Using type() method. type() method returns the datatype of variable.
Python3
# Python3 code to demonstrate working of
# Data type frequency in tuple
# 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
count = 0
for ele in test_tuple:
if type(ele) is data_type:
count+=1
# printing result
print("The data type frequency : " + str(count))
OutputThe original tuple : (5, 'Gfg', 2, 8.8, 1.2, 'is')
The data type frequency : 2
Method#4: Using filter()
Python3
# Python3 code to demonstrate working of
# Data type frequency in tuple
# 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 filter
count = len(list(filter(lambda ele: type(ele) is data_type, test_tuple)))
# printing result
print("The data type frequency : " + str(count))
#This code is contributed by Vinay Pinjala.
OutputThe original tuple : (5, 'Gfg', 2, 8.8, 1.2, 'is')
The data type frequency : 2
Time complexity: O(n)
Space complexity: O(1)
Method#5: Using Recursive method.
Python3
# Python3 code to demonstrate working of
# Data type frequency in tuple
#using recursive method
def count_data_type(tup, data_type, count=0):
if not tup: #base condition
return count
if type(tup[0]) is data_type:
count += 1
return count_data_type(tup[1:], data_type, count)
# 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
count = count_data_type(test_tuple, data_type)
# printing result
print("The data type frequency:", count)
#this code contributed by tvsk.
OutputThe original tuple : (5, 'Gfg', 2, 8.8, 1.2, 'is')
The data type frequency: 2
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #6: Using a reduce() function and lambda function
Algorithm:
1.Import the reduce function from the functools module.
2.Initialize the input tuple and the data type whose frequency is to be counted.
3.Use the reduce() function to count the number of elements in the tuple that are of the specified data type. The reduce() function applies the 4.lambda function to each element of the tuple, which checks whether the element is of the specified data type. If it is, it increments the count by 1.
5.Print the count of the specified data type in the tuple.
Python3
from functools import reduce
# 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 reduce
count = reduce(lambda x, y: x + isinstance(y, data_type), test_tuple, 0)
# printing result
print("The data type frequency : " + str(count))
#This code is contributed by Jyothi pinjala
OutputThe original tuple : (5, 'Gfg', 2, 8.8, 1.2, 'is')
The data type frequency : 2
Time Complexity: O(n), where n is the number of elements in the tuple. The reduce() function iterates over each element of the tuple once.
Auxiliary Space: O(1), since only constant extra space is used to store the count variable.
Method #7: Using list comprehension and the built-in function type():
Step-by-step approach:
- Initialize a tuple test_tuple.
- Print the original tuple.
- Initialize the data type data_type as float.
- Use a list comprehension to create a list of all elements in test_tuple that have the same data type as data_type.
- Find the length of the list generated in step 4 using the built-in function len().
- Print the result.
Python3
# 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 list comprehension and type()
count = len([x for x in test_tuple if type(x) == data_type])
# printing result
print("The data type frequency : " + str(count))
OutputThe original tuple : (5, 'Gfg', 2, 8.8, 1.2, 'is')
The data type frequency : 2
Time complexity: O(n), where n is the length of the tuple.
Auxiliary space: O(k), where k is the number of elements in the tuple that have the same data type as data_type.
Similar Reads
Python | Finding frequency in list of tuples
In python we need to handle various forms of data and one among them is list of tuples in which we may have to perform any kind of operation. This particular article discusses the ways of finding the frequency of the 1st element in list of tuple which can be extended to any index. Let's discuss cert
6 min read
Python | Find sum of frequency of given elements in the list
Given two lists containing integers, the task is to find the sum of the frequency of elements of the first list in the second list. Example: Input: list1 = [1, 2, 3] list2 = [2, 1, 2, 1, 3, 5, 2, 3] Output: 7 Explanation: No of time 1 occurring in list2 is :2 No of time 2 occurring in list2 is :3 No
4 min read
Python - Count frequency of Sublist in given list
Given a List and a sublist, count occurrence of sublist in list. Input : test_list = [4, 5, 3, 5, 7, 8, 3, 5, 7, 2, 3, 5, 7], sublist = [3, 5, 7] Output : 3 Explanation : 3, 5, 7 occurs 3 times. Input : test_list = [4, 5, 3, 5, 8, 8, 3, 2, 7, 2, 3, 6, 7], sublist = [3, 5, 7] Output : 0 Explanation :
3 min read
Python - Elements frequency in Tuple
Given a Tuple, find the frequency of each element. Input : test_tup = (4, 5, 4, 5, 6, 6, 5) Output : {4: 2, 5: 3, 6: 2} Explanation : Frequency of 4 is 2 and so on.. Input : test_tup = (4, 5, 4, 5, 6, 6, 6) Output : {4: 2, 5: 2, 6: 3} Explanation : Frequency of 4 is 2 and so on.. Method #1 Using def
7 min read
Python - Elements frequency in Tuple Matrix
Sometimes, while working with Python Tuple Matrix, we can have a problem in which we need to get the frequency of each element in it. This kind of problem can occur in domains such as day-day programming and web development domains. Let's discuss certain ways in which this problem can be solved. Inp
5 min read
Python - Maximum frequency in Tuple
Sometimes, while working with Python tuples, we can have a problem in which we need to find the maximum frequency element in the tuple. Tuple, being quite a popular container, this type of problem is common across the web development domain. Let's discuss certain ways in which this task can be perfo
5 min read
Python - Step Frequency of elements in List
Sometimes, while working with Python, we can have a problem in which we need to compute frequency in list. This is quite common problem and can have usecase in many domains. But we can atimes have problem in which we need incremental count of elements in list. Let's discuss certain ways in which thi
4 min read
Python | Frequency of substring in given string
Finding a substring in a string has been dealt with in many ways. But sometimes, we are just interested to know how many times a particular substring occurs in a string. Let's discuss certain ways in which this task is performed. Method #1: Using count() This is a quite straightforward method in whi
6 min read
Python - Elements Frequency in Mixed Nested Tuple
Sometimes, while working with Python data, we can have a problem in which we have data in the form of nested and non-nested forms inside a single tuple, and we wish to count the element frequency in them. This kind of problem can come in domains such as web development and Data Science. Let's discus
8 min read
Python | Find number of lists in a tuple
Given a tuple of lists, the task is to find number of lists in a tuple. This is a very basic problem but can be useful while making some utility application. Method #1: Using len Python3 # Python code to find number of list in a tuple # Initial list Input1 = ([1, 2, 3, 4], [5, 6, 7, 8]) Input2 = ([1
4 min read