# Python | Count the elements till first tuple

Sometimes, 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 which 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 + `isintance() + 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()`.

 `# 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)) `

Output :

```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 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 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)) `

Output :

```The original tuple : (1, 5, 7, (4, 6), 10)
Elements till the first tuple : 3
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.