Count elements before a tuple
Write a Python program to count the elements of a list until the first tuple is reached.
[1, 6, 8, 5, (4, 6, 8, 0), 5, 4]
4
Approach
- Count until
isinstance(x, tuple).
Solution
data = [1, 6, 8, 5, (4, 6, 8, 0), 5, 4]count = 0for x in data: if isinstance(x, tuple): break count += 1print(count)4
Learn the concept
New to this topic? Read the lesson, then come back.