Python Practice › Tuple Programs › Program 40

Count elements before a tuple

EasyTuple Programs · 40 of 70

Write a Python program to count the elements of a list until the first tuple is reached.

Advertisement
Input
[1, 6, 8, 5, (4, 6, 8, 0), 5, 4]
Output
4

Approach

  1. Count until isinstance(x, tuple).

Solution

Python
data = [1, 6, 8, 5, (4, 6, 8, 0), 5, 4]count = 0for x in data:    if isinstance(x, tuple):        break    count += 1print(count)
Output
4
Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More tuple programs

← All 70 Tuple Programs