Python | Count the elements in the list until the element becomes a tuple

Counters | Python Methods and Functions

Examples:

 Input: [4, 5, 6, 10, (1, 2, 3), 11, 2, 4] Output: 4 Input: [4, (5, 6) , 10, (1, 2, 3), 11, 2, 4] Output: 1 

In this program we will use the concept isinstance () to check if we encounter a tuple or not on our counting path. For a detailed tutorial on isinstance () visit

# Python item counting program
# until a list is encountered

def Count (li):

counter = 0

for num in li:

  if isinstance (num, tuple ):

  break

counter = counter + 1

return counter

 
Driver code

li = [ 4 , 5 , 6 , 10 , ( 1 , 2 , 3 ), 11 , 2 , 4 ]

print (Count (li))

Output:

 4 




Tutorials