Method # 1: Using the loop + isintance () + enumerate ()
This problem can be solved using the above functions. In this we just iterate over the elements using enumerate ()
to get its index and check the type using isinstance ()
.

Output:
The original tuple: (1, 5, 7, (4, 6), 10) Elements till the first tuple: 3
Method # 2: Using takewhile () + sum () + isinstance ()
+ lambda
A combination of the above functions can also be used to solve this problem. In this we use takewhile ()
to iterate to the tuple and sum ()
to check the counter.
# Python3 code to demonstrate how it works
# Elements before the first tuple
# using takewhile () + sum () + isinstance () + lambda
from
itertools
import
takewhile
# initialize the tuple
test_tup
=
(
1
,
5
,
7
, (
4
,
6
),
10
)
# print the original tuple
print
(
"The original tuple:"
+
str
( test_tup))
# Elements before the first tuple
# using takewhile () + sum () + isinstance () + lambda
res
=
sum
(
1 for
sub
in
takewhile (
< code class = "keyword"> lambda
ele:
not
isinstance
(ele,
tuple
), test_tup))
# print 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
This encyclopedia will be an indispensable resource for our time as it reflects the fact that we are currently living in an expanding datadriven world. ...
28/08/2021
This book project was first presented to me during my first week in my current role of managing the data mining development at SAS. Writ ing a book has always been a bucket‐list item, and I was ver...
10/07/2020
Computer Age Statistical Inference: Algorithms, Evidence, and Data Science (Institute of Mathematical Statistics Monographs, Series Number 6). The twentyfirst century has seen a breathtaking expan...
28/08/2021
A Practical Approach to Computer Algorithms Using Python® and C# Rod Stephens started out as a mathematician, but while studying at MIT, he discovered how much fun algorithms are. He took every al...
23/09/2020