ADVERTISEMENT
ADVERTISEMENT

Python program to count all the elements till first tuple

In this program, we are given a tuple of elements consisting of nested tuples. We need to create a program to count all the elements till the first tuple in Python.
Submitted by Shivang Yadav, on January 04, 2022

Python programming language has a lot of utility function for different types of collection and their combination. Checking for the presence of any collection inside a collection is important in cases when the program consists of nested collections. Here, we will see a program to count the number of other elements that are present in the collection before the first tuple is encountered.

Before going further with the problem, let's recap some basic topics that will help in understanding the solution.

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.

Example:

tuple = ("python", "includehelp", 43, 54.23)

Counting all the elements till first Tuple

In the program, we are given a nested collection consisting of a tuple. And we need to create a Python program to count all the elements till the first tuple is encountered.

Input:
(4, 6, (1, 2, 3), 7, 9, (5, 2))

Output:
2

For this, we will traverse the collection till the first tuple is encountered. And count the new number of elements till the tuple. This can be done in Python using a combination of different methods.

Method 1:

One method to solve the problem is by looping over the collection using enumerate() which also returns the count and breaks the loop when a tuple is encountered checked using isinstance() method. The returned counter value of the enumerate() method is the required value.

# Python program to count all 
# the elements till first tuple

# Initializing and printing tuple 
myTuple = (4, 6, (1, 2, 3), 7, 9, (5, 2))
print("The elements of tuple are " + str(myTuple))

# Counting all elements till first Tuple 
for count, ele in enumerate(myTuple):
	if isinstance(ele, tuple):
		break

print("The count of elements before first tuple is " + str(count))

Output:

The elements of tuple are (4, 6, (1, 2, 3), 7, 9, (5, 2))
The count of elements before first tuple is 2

Method 2:

Another method to solve the problem is by using a combination of takewhile() and sum method. The takewhile() method takes a lambda function to check if the element is not a tuple.

# Python program to count all 
# the elements till first tuple

from itertools import takewhile

# Initializing and printing tuple 
myTuple = (4, 6, (1, 2, 3), 7, 9, (5, 2))
print("The elements of tuple are " + str(myTuple))

# Counting all elements till first Tuple 
count = sum(1 for sub in takewhile(lambda val: not isinstance(val, tuple), myTuple))

print("The count of elements before first tuple is " + str(count))

Output:

The elements of tuple are (4, 6, (1, 2, 3), 7, 9, (5, 2))
The count of elements before first tuple is 2

Python Tuple Programs »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.