Python | Count the elements till first tuple
AuxiliSometimes, while working with records, we can have a problem in which an element of a record is another tuple records and we might have to count the element count that occur before the record. This is a problem that does not occur commonly, but having a solution to it is useful. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using loop + isinstance() + enumerate()
This problem can be solved using the above functionalities. In this, we just loop through the elements using enumerate() to get the index count of it and check the type using isinstance() method.
Python3
# Python3 code to demonstrate working of # Elements till first tuple # using isinstance() + enumerate() + loop # initialize tuple test_tup = ( 1 , 5 , 7 , ( 4 , 6 ), 10 ) # printing original tuple print ( "The original tuple : " + str (test_tup)) # Elements till first tuple # using isinstance() + enumerate() + loop for count, ele in enumerate (test_tup): if isinstance (ele, tuple ): break # printing result print ( "Elements till the first tuple : " + str (count)) |
The original tuple : (1, 5, 7, (4, 6), 10) Elements till the first tuple : 3
Method #2: Using takewhile() + sum() + isinstance() + lambda
The combination of the above functions can also be used to solve this problem. In this, we use takewhile(), to iterate till a tuple and sum() to check the counter.
Python3
# Python3 code to demonstrate working of # Elements till first tuple # using takewhile() + sum() + isinstance() + lambda from itertools import takewhile # initialize tuple test_tup = ( 1 , 5 , 7 , ( 4 , 6 ), 10 ) # printing original tuple print ( "The original tuple : " + str (test_tup)) # Elements till first tuple # using takewhile() + sum() + isinstance() + lambda res = sum ( 1 for sub in takewhile( lambda ele: not isinstance (ele, tuple ), test_tup)) # printing result print ( "Elements till the first tuple : " + str (res)) |
The original tuple : (1, 5, 7, (4, 6), 10) Elements till the first tuple : 3
Method #3 : Using type() method
Python3
# Python3 code to demonstrate working of # Elements till first tuple # initialize tuple test_tup = ( 1 , 5 , 7 , ( 4 , 6 ), 10 ) # printing original tuple print ( "The original tuple : " + str (test_tup)) c = 0 for i in test_tup: if ( type (i) is tuple ): break else : c + = 1 # printing result print ( "Elements till the first tuple : " + str (c)) |
The original tuple : (1, 5, 7, (4, 6), 10) Elements till the first tuple : 3
Method #4 : Using next() and isinstance() function
This problem can be solved using the above functionalities. In this, we use the next() function to iterate over the elements in the tuple and check the type using isinstance() method.
Python3
# Python3 code to demonstrate working of # Elements till first tuple # using next() and lambda function # initialize tuple test_tup = ( 1 , 5 , 7 , ( 4 , 6 ), 10 ) # printing original tuple print ( "The original tuple : " + str (test_tup)) # Elements till first tuple # using next() and lambda function res = next (i for i, ele in enumerate (test_tup) if isinstance (ele, tuple )) # printing result print ( "Elements till the first tuple : " + str (res)) #This code is contributed by Edula Vinay Kumar Reddy |
The original tuple : (1, 5, 7, (4, 6), 10) Elements till the first tuple : 3
Time complexity: O(n)
Auxiliary Space: O(1)
In this method, we are using next() function which returns the next item from the iterator. We are passing an iterator which is a generator expression, it will return the index of the first tuple in the tuple. So, it is not creating any extra space, so the Auxiliary space is O(1) . We are iterating through all the elements of the tuple, so the time complexity is O(n).
METHOD 5:Using slicing
APPROACH:
This approach uses the index() method to find the index of the first occurrence of a tuple in the given tuple. It then uses slicing to extract the elements from the start of the tuple up to the index value, and returns the length of this slice as the count of non-tuple elements.
ALGORITHM:
1.Find the index of the first occurrence of a tuple using the index() method
2.Slice the tuple from the start to the index value
3.Find the length of the sliced tuple
4.Return the length value
Python3
def count_elements(tup): index = tup.index( next ( filter ( lambda x: isinstance (x, tuple ), tup))) return len (tup[:index]) tup = ( 1 , 5 , 7 , ( 4 , 6 ), 10 ) print (count_elements(tup)) |
3
Time complexity: O(n) where n is the number of elements in the tuple
Auxiliary Space: O(n) since the sliced tuple is stored in memory
Contact Us