- 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.
- Python – Test if list is Palindrome
- Python – Check if any list element is present in Tuple
- Test if Tuple contains K in Python
- Test if tuple is distinct in Python
- Python – Extract Kth element of every Nth tuple in List
- Check if tuple has any None value in Python
- Python – Cross Pairing in Tuple List
- Python – Test if all rows contain any common element with other Matrix
- Python – Test if elements of list are in Min/Max range from other list
- Maximum element in tuple list in Python
- Python – Test if Rows have Similar frequency
- Python – Concatenate Rear elements in Tuple List
- Update each element in tuple list in Python
- Python – Test for Word construction from character list