
- 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
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 Articles
- 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
- Program to check whether all leaves are at same level or not in Python
- How to encode multiple strings that have the same length using Tensorflow and Python?
- Program to check whether all palindromic substrings are of odd length or not in Python
- Program to find sum of all odd length subarrays in Python
- Program to find all words which share same first letters in Python
- Python - Split list into all possible tuple pairs
- Program to find all upside down numbers of length n in Python
- Program to find maximum profit after cutting rods and selling same length rods in Python
- Program to find length of longest contiguous sublist with same first letter words 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
- Find Maximum difference between tuple pairs in Python

Advertisements