In this python tuple program, we will count the elements in a list if an element is a tuple.
Steps to solve the program
- Take a list as input.
- Use a for loop to iterate over each element in the list.
- If the type of an element is a tuple then count the number of elements in it and print the output.
- Use type() and len() for this purpose.
- Print the output.
l = [1,6,8,5,(4,6,8,0),5,4]
print("Original list: ",l)
for val in l:
if type(val) is tuple:
print(f"Elements in the tuple {val}: ",len(val))
Output :
Original list: [1, 6, 8, 5, (4, 6, 8, 0), 5, 4]
Elements in the tuple (4, 6, 8, 0): 4