Python - Count elements in record tuple
Last Updated :
08 May, 2023
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 #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 of elements using len().
Python3
# Python3 code to demonstrate working of
# Record 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))
# Record elements count
# using len() + generator expression
res = len(list((int(j) for i in test_list for j in i)))
# printing result
print("The total count of list is : " + str(res))
Output : The original list : [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
The total count of list is : 10
Time Complexity: O(n) where n is the number of elements in the list “test_list”. len() + generator expression performs n number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the list
Method #2 : Using len() + map() + chain.from_iterable() The combination of above methods can also be used to perform this task. In this, the extension of finding total count is done by the combination of map() and from_iterable().
Python3
# Python3 code to demonstrate working of
# Record 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))
# Record elements count
# using len() + map() + chain.from_iterable()
res = len(list((map(int, chain.from_iterable(test_list)))))
# printing result
print("The total count of list is : " + str(res))
Output : The original list : [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
The total count of list is : 10
Time Complexity: O(n*n), where n is the length of the input list. This is because we’re using len() + map() + chain.from_iterable() which has a time complexity of O(n*n) in the worst case.
Auxiliary Space: O(1), as we’re using constant additional space
Method #3 : Using extend(),list(),len() methods
Python3
# Python3 code to demonstrate working of
# Record elements count
# initialize list
test_list = [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
# printing original list
print("The original list : " + str(test_list))
res=[]
for i in test_list:
res.extend(list(i))
# printing result
print("The total count of list is : " + str(len(res)))
OutputThe original list : [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
The total count of list is : 10
Time complexity: O(n), where n is the length of the test_list.
Auxiliary Space: O(n), where n is the length of the test_list.
Method 4: Using sum() and map()
Python3
# Python3 code to demonstrate working of
# Record elements count
# using sum() and map()
# initialize list
test_list = [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
# printing original list
print("The original list : " + str(test_list))
# Record elements count
# using sum() and map()
res = sum(map(len, test_list))
# printing result
print("The total count of list is : " + 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 total count of list is : 10
In this method, we use the built-in sum() function and map() function to iterate over the list and get the length of each tuple, and finally sum up all the lengths using the sum() function.
Time Complexity: O(n) where n is the number of elements in the list
Space Complexity: O(1) as we are only storing a single variable res
METHOD 5:Using loop.
APPROACH:
This program counts the total number of elements present in a list of tuples.
ALGORITHM:
1. Initialize a variable count to 0
2. Loop through each tuple in the list of tuples list1
3. Add the length of each tuple to the count variable
4. Print the value of the count variable
Python3
list1 = [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
count = 0
for tup in list1:
count += len(tup)
print(f"The total count of list is: {count}")
OutputThe total count of list is: 10
Time Complexity: O(n), where n is the total number of tuples in the list
Space Complexity: O(1), as only one extra variable (count) is used. The space used by the input list is not counted as extra space.
Similar Reads
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 | 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 | 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 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 - Sum of tuple elements Sometimes, while programming, we have a problem in which we might need to perform summation among tuple elements. This is an essential utility as we come across summation operations many times and tuples are immutable and hence required to be dealt with. Letâs discuss certain ways in which this task
6 min read