Python - Count elements in tuple list
Last Updated :
09 Apr, 2023
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() + generator expression This is the most basic method to achieve solution to this task. In this, we iterate over whole nested lists using generator expression and get the count using len().
Python3
# Python3 code to demonstrate working of
# Tuple list elements count
# using len() + generator expression
# initialize list
test_list = [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
# printing original list
print("The original list : " + str(test_list))
# Tuple list elements count
# using len() + generator expression
temp = list((int(j) for i in test_list for j in i))
res = len(temp)
# printing result
print("The tuple list elements count : " + str(res))
Output : The original list : [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
The tuple list elements count : 10
Time Complexity: O(n) where n is the number of elements in the string list. The len() + generator expression is used to perform the task and it takes O(n) time.
Auxiliary Space: O(1) constant additional space is created.
Method #2 : Using len() + map() + chain.from_iterable() The combination of the above methods can also be used to perform this task. In this, the extension of the finding count is done by a combination of map() and from_iterable().
Python3
# Python3 code to demonstrate working of
# Tuple list elements count
# using len() + map() + chain.from_iterable()
from itertools import chain
# initialize list
test_list = [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
# printing original list
print("The original list : " + str(test_list))
# Tuple list elements count
# using len() + map() + chain.from_iterable()
res = len(list(map(int, chain.from_iterable(test_list))))
# printing result
print("The tuple list elements count : " + str(res))
Output : The original list : [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
The tuple list elements count : 10
Time Complexity: O(n*n) where n is the number of elements in the string list. The rlen() + map() + chain.from_iterable() is used to perform the task and it takes O(n*n) time.
Auxiliary Space: O(1) constant additional space is required
Method #3: Using list comprehension
Python3
test_list = [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
x=[j for i in test_list for j in i]
print(len(x))
Method #4: Using enumerate function
Python3
test_list = [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
x = [j for i in enumerate(test_list) for j in i]
print(len(x))
Method #5 : Using sum() + list comprehension
This method uses sum() function to calculate the number of elements in the tuple list. It uses list comprehension to iterate through the elements of the tuple list and increment the count.
Python3
# Python3 code to demonstrate working of
# Tuple list elements count
# using sum() + list comprehension
# initialize list
test_list = [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
# printing original list
print("The original list : " + str(test_list))
# Tuple list elements count
# using sum() + list comprehension
res = sum([len(i) for i in test_list])
# printing result
print("The tuple list elements count : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy
OutputThe original list : [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
The tuple list elements count : 10
This method uses a list comprehension to find the length of each tuple in the list, and then uses the sum() function to find the total number of elements across all the tuples.
Time Complexity: O(n) where n is the number of tuples in the list
Auxiliary Space: O(1) as no extra space is being used.
Method #6:Using reduce
Algorithm:
- Initialize a variable count to 0.
- Loop through each tuple t in the list.
- Get the length of the tuple t using the len() function.
- Add the length of the tuple t to the count variable.
- Return the count variable.
Python3
# import reduce from functools module
from functools import reduce
# initialize list
test_list = [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
# printing original list
print("The original list : " + str(test_list))
# Tuple list elements count using reduce()
res = reduce(lambda x, y: x + len(y), test_list, 0)
# printing result
print("The tuple list elements count : " + str(res))
#This code is contributed by Vinay Pinjala.
OutputThe original list : [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
The tuple list elements count : 10
Time Complexity:
The time complexity of this algorithm is O(n), where n is the number of tuples in the list. This is because we are iterating through each tuple in the list once to get the length of the tuples and add them to the count variable.
Space Complexity:
The space complexity of this algorithm is O(1), because we are not using any additional data structures to store the elements of the list or intermediate values. We are only using a single variable count to store the count of elements in the tuples. Therefore, the space used by the algorithm is constant and does not depend on the size of the input list.
Similar Reads
Python | Counting Nth tuple element Sometimes, while working with Python, we can have a problem in which we need to count the occurrence of a particular's elements. This kind of problem is quite common while working with records. Let's discuss a way in which this task can be performed. Method #1 : Using Counter() + generator expressio
5 min read
Python - Count elements in record tuple Sometimes, while working with data in form of records, we can have a problem in which we need to find the total element counts 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
5 min read
Python | List Element Count with Order Sometimes, while working with lists or numbers we can have a problem in which we need to attach with each element of list, a number, which is the position of that element's occurrence in that list. This type of problem can come across many domains. Let's discuss a way in which this problem can be so
4 min read
Python | Count occurrence of all elements of list in a tuple 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 Appro
5 min read
Python | Tuple Column element frequency In Python, we need to handle various forms of data and one among them is a 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 Kth element in the list of tuples. Letâs discuss certain ways in which this can
5 min read
Python - Check if Tuple contains only K elements Sometimes, while working with Python tuples, we can have a problem in which we need to test if any tuple contains elements from set K elements. This kind of problem is quite common and have application in many domains such as web development and day-day programming. Let's discuss certain ways in whi
3 min read
Python | Matching elements count Sometimes, while working with lists we need to handle two lists and search for the matches, and return just the count of indices of the match. Querying whole list for the this process is not feasible when the size of master list is very large, hence having just the match indices helps in this cause.
5 min read
Python Tuple count() Method In this article, we will learn about the count() method used for tuples in Python. The count() method of a Tuple returns the number of times the given element appears in the tuple. Example Python3 tuple = (1, 2, 3, 1, 2, 3, 1, 2, 3) print(tuple.count(3)) Output : 3Python Tuple count() Method Syntax
3 min read
Python | Extend tuples by count of elements in tuple Sometimes, while working with data, we can have an application in which we need to duplicate tuple elements by the amount of element count. This is very unique application but can occur in certain cases. Let's discuss certain ways in which this task can be performed. Method #1: Using nested loops Th
6 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