A is a given list. This list has nested tuples. Our task is to count the elements in a list until an element is a tuple. Here we use isinstance() function. This function has two parameters object and classinfo.object is to be checked and classinfo is class, type or tuple of class and types. This function returns true if the object is an instance or subclass of as class, or any element of the tuple and false otherwise.
Input : A=[4, 5, 6, 10,22,33, (1, 2, 3), 11, 2, 4] Output : 6
Step 1: Given a list. Step 2: Use one counter variable c which is initialized by 0. Step 3: We traverse the list and verify that encountering a tuple or not in our path of count. Step 4: If it’s true then counter will be increased by 1 otherwise false. Step 5: return c
# Program to count the items # until a list is encountered a tuple def countelement(M): c = 0 print("RESULT ::>") for i in M: if isinstance(i, tuple): break c = c + 1 return c # Driver Code A = [4, 5, 6, 10,22,33, (1, 2, 3), 11, 2, 4] print(countelement(A))