Python | Count occurrence of all elements of list in a tuple
Last Updated :
21 Mar, 2023
Given a tuple and a list as input, write a Python program to count the occurrences of all items of the list in the tuple. Examples:
Input : tuple = ('a', 'a', 'c', 'b', 'd')
list = ['a', 'b']
Output : 3
Input : tuple = (1, 2, 3, 1, 4, 6, 7, 1, 4)
list = [1, 4, 7]
Output : 6
Approach #1 : Naive Approach The first approach is the naive approach. Use a for loop and traverse through the given list and count the occurrence of each item of tuple in a list. Finally, return the count.
Python3
# Python3 Program to count occurrence
# of all elements of list in a tuple
from collections import Counter
def countOccurrence(tup, lst):
count = 0
for item in tup:
if item in lst:
count+= 1
return count
# Driver Code
tup = ('a', 'a', 'c', 'b', 'd')
lst = ['a', 'b']
print(countOccurrence(tup, lst))
Time Complexity: O(n)
Auxiliary Space: O(n), where n is length of tuple.
Approach #2 : Using Counter From Python Collections module, import counter to solve the given problem. A Counter is a container that keeps track of how many times equivalent values are added. Having saved the resultant in 'counts', we use a for loop and count how many times each item in list occurs in 'counts' and sum it to give the final output.
Python3
# Python3 Program to count occurrence
# of all elements of list in a tuple
from collections import Counter
def countOccurrence(tup, lst):
counts = Counter(tup)
return sum(counts[i] for i in lst)
# Driver Code
tup = ('a', 'a', 'c', 'b', 'd')
lst = ['a', 'b']
print(countOccurrence(tup, lst))
Approach #3 : Using Set Another method of solving the given problem is using set data structure. Simply convert the given list into a set, which removes all duplicates. And now, for each item of list, count its occurrence in tuple and sum them.
Python3
# Python3 Program to count occurrence
# of all elements of list in a tuple
def countOccurrence(tup, lst):
lst = set(lst)
return sum(1 for x in tup if x in lst)
# Driver Code
tup = ('a', 'a', 'c', 'b', 'd')
lst = ['a', 'b']
print(countOccurrence(tup, lst))
Approach #4 : Using Python dictionary Get each item of tuple and its frequency as key:value pair in Python dictionary, then using a for loop, for each item of list, count its occurrence in tuple and sum them.
Python3
# Python3 Program to count occurrence
# of all elements of list in a tuple
def countOccurrence(tup, lst):
dct = {}
for i in tup:
if not dct.get(i):
dct[i] = 0
dct[i] += 1
return sum(dct.get(i, 0) for i in lst)
# Driver Code
tup = ('a', 'a', 'c', 'b', 'd')
lst = ['a', 'b']
print(countOccurrence(tup, lst))
Approach #5 : Python numpy.in1d() Python numpy gives us a direct method to find the solution for the given problem, and that is numpy.in1d(). This method test whether each element of a 1-D array is also present in a second array. Since list is also a 1-D array, this method can be applied here.
Python3
# Python3 Program to count occurrence
# of all elements of list in a tuple
import numpy as np
def countOccurrence(tup, lst):
return np.in1d(tup, lst).sum()
# Driver Code
tup = ('a', 'a', 'c', 'b', 'd')
lst = ['a', 'b']
print(countOccurrence(tup, lst))
Approach #6 : Using list() and count() methods
Python3
# Python3 Program to count occurrence
# of all elements of list in a tuple
tup = ('a', 'a', 'c', 'b', 'd')
lst = ['a', 'b']
x=list(tup)
c=0
for i in lst:
c+=x.count(i)
print(c)
Approach#7: Using a list comprehension and the count() function
Algorithm:
- Initialize a tuple tup and a list lst.
- Create a list comprehension that loops through each element i in lst and counts the number of occurrences of i in the tup using the count() method.
- Compute the sum of the counts obtained in step 2.
- Assign the result of step 3 to the variable count.
- Print the value of the count variable.
Python3
tup = ('a', 'a', 'c', 'b', 'd')
lst = ['a', 'b']
count = sum([tup.count(i) for i in lst])
print(count)
#This code is contributed by Vinay Pinjala.
Time Complexity: The time complexity of the list comprehension is O(n^2) because for each element of lst, it counts the number of occurrences of that element in the tup, which involves iterating over all elements of the tup. However, since there are only two elements in lst, the time complexity of the entire algorithm is O(n), where n is the length of the tup.
Space Complexity: The space complexity of the algorithm is O(n), where n is the length of the tup, because the list comprehension generates a list of counts that is proportional to the length of the tup. However, the space used by the lst and count variables is constant and does not depend on the length of the tup.
Similar Reads
Element Occurrence in Dictionary of List Values - Python We are having a dictionary of list we need to find occurrence of all elements. For example, d = {'a': [1, 2, 3, 1], 'b': [3, 4, 1], 'c': [1, 5, 6]} we need to count the occurrence of all elements in dictionary list so that resultant output should be {'a': 2, 'b': 1, 'c': 1}.Using a Dictionary Compre
3 min read
Python - Sum of each List element occurrence in another Sometimes, while working with Python, we can have a problem in which we need to get occurrence of 1 element in another. But as a modification of this, we can have a problem in which we need to count the occurrence of all elements of 1 list in another. Lets discuss certain ways in which this task can
6 min read
Python | Count tuples occurrence in list of tuples Many a time while developing web and desktop products in Python, we use nested lists and have several queries about how to find the count of unique tuples. Let us see how to get the count of unique tuples in the given list of tuples. Below are some ways to achieve the above task. Method #1: Using It
5 min read
Python - Count elements in tuple list Sometimes, while working with data in form of records, we can have a problem in which we need to find the count of all the records received. This is a very common application that can occur in Data Science domain. Letâs discuss certain ways in which this task can be performed. Method #1: Using len()
5 min read
Python - Occurrence counter in List of Records Sometimes, while dealing with records we can have a problem in which we need count the occurrence of incoming digits corresponding to different characters/players in a game and compile them in a dictionary. This can have application in gaming and web development. Lets discuss a way in which this can
2 min read