- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
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: 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")
The list is : [(72, 72, 72), (72, 72), (72, 72)] The tuple contains a single element
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.
- Test if Tuple contains K in Python
- Test if tuple is distinct in Python
- Python – Check if any list element is present in Tuple
- Check if tuple has any None value in Python
- Python – Test if list is Palindrome
- Maximum element in tuple list in Python
- Update each element in tuple list in Python
- Check if element is present in tuple in Python
- Check if tuple and list are identical in Python
- How to test if a List is an Unmodifable List in Java?
- Check if element is present in tuple of tuples in Python
- Test if element is present using Selenium WebDriver?
- Python – Extract Kth element of every Nth tuple in List
- Python - Join tuple elements in a list