

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find whether all tuple have same length in Python
In this article we will find out if all the tuples in a given list are of same length.
With len
We will use len function and compare its result to a given value which we are validating. If the values are equal then we consider them as same length else not.
Example
listA = [('Mon', '2 pm', 'Physics'), ('Tue', '11 am','Maths')] # printing print("Given list of tuples:\n", listA) # check length k = 3 res = 1 # Iteration for tuple in listA: if len(tuple) != k: res = 0 break # Checking if res is true if res: print("Each tuple has same length") else: print("All tuples are not of same length")
Output
Running the above code gives us the following result −
Given list of tuples: [('Mon', '2 pm', 'Physics'), ('Tue', '11 am', 'Maths')] Each tuple has same length
With all and len
We sue the len function alogn with the all function and use a for loop to iterate through each of the tuple present in the list.
Example
listA = [('Mon', '2 pm', 'Physics'), ('Tue', '11 am','Maths')] # printing print("Given list of tuples:\n", listA) # check length k = 3 res=(all(len(elem) == k for elem in listA)) # Checking if res is true if res: print("Each tuple has same length") else: print("All tuples are not of same length")
Output
Running the above code gives us the following result −
Given list of tuples: [('Mon', '2 pm', 'Physics'), ('Tue', '11 am', 'Maths')] Each tuple has same length
- Related Questions & Answers
- Python – Filter tuple with all same elements
- Program to find tuple with same product in Python
- Program to find maximum length of k ribbons of same length in Python
- How to encode multiple strings that have the same length using Tensorflow and Python?
- Program to check whether all leaves are at same level or not in Python
- Program to find sum of all odd length subarrays in Python
- Program to check whether all palindromic substrings are of odd length or not in Python
- Program to find all words which share same first letters in Python
- Program to find all upside down numbers of length n in Python
- Find rows that have the same value on a column in MySQL?
- Program to find length of longest contiguous sublist with same first letter words in Python
- Program to find maximum profit after cutting rods and selling same length rods in Python
- Tuple with the same Product in C++
- Program to check whether we can color a tree where no adjacent nodes have the same color or not in python
- Program to find length of shortest sublist with maximum frequent element with same frequency in Python
Advertisements