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() + 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)) |
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)) |
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)) |
10
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)) |
10
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 |
The 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. |
The 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.
Contact Us