
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python – Test if tuple list has a single element
When it is required to test if a tuple list contains a single element, a flag value and a simple iteration is used.
Example
Below is a demonstration of the same
my_list = [(72, 72, 72), (72, 72), (72, 72)] print("The list is :") print(my_list) my_result = True for sub in my_list: flag = True for element in sub: if element != my_list[0][0]: flag = False break if not flag: my_result = False break if(flag == True): print("The tuple contains a single element") else: print("The tuple doesn't contain a single element")
Output
The list is : [(72, 72, 72), (72, 72), (72, 72)] The tuple contains a single element
Explanation
A list of list is defined and is displayed on the console.
A variable is assigned to ‘True’.
The list is iterated over, and a value is flagged as ‘True’.
If an element of the list is not equal to the first element of the list, the value is flagged to ‘False’.
Otherwise, the variable is changed to ‘False.
The control is broken outpf the loop.
Outside the method, if the flagged value is ‘True’, it means the list contains a single element only.
Relevant message is displayed on the console.
- Related Articles
- Python – Check if any list element is present in Tuple
- Python Program to Test if string contains element from list
- Test if Tuple contains K in Python
- Test if tuple is distinct in Python
- Maximum element in tuple list in Python
- Python Program to Test if any set element exists in List
- Update each element in tuple list in Python
- Python – Test if list is Palindrome
- How to test if an element has class using Protractor?
- Check if tuple has any None value in Python
- Python – Extract Kth element of every Nth tuple in List
- Check if element is present in tuple in Python
- Check if tuple and list are identical in Python
- Python program to count the elements in a list until an element is a Tuple?
- Flatten tuple of List to tuple in Python

Advertisements